Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a continuous animation
    text
    copied!<p>I want to create an animation that moves up or down the screen according to how fast the user taps the screen. The problem I am having is that I don't know how to create an infinite loop so I am firing a timer which presents issues. Here is my current code.</p> <pre><code>-(void)setPosOfCider { CGFloat originalY = CGRectGetMinY(cider.frame); float oY = originalY; float posY = averageTapsPerSecond * 100; float dur = 0; dur = (oY - posY) / 100; [UIImageView animateWithDuration:dur animations:^(void) { cider.frame = CGRectMake(768, 1024 - posY, 768, 1024); }]; } </code></pre> <p>Suggested fix(Doesn't work):</p> <pre><code> - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. scroll.pagingEnabled = YES; scroll.scrollEnabled = YES; scroll.contentSize = CGSizeMake(768 * 3, 1024); // 3 pages wide. scroll.delegate = self; self.speedInPointsPerSecond = 200000; self.tapEvents = [NSMutableArray array]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self startDisplayLink]; } -(IBAction)tapped { [self.tapEvents addObject:[NSDate date]]; // if less than two taps, no average speed if ([self.tapEvents count] &lt; 1) return; // only average the last three taps if ([self.tapEvents count] &gt; 2) [self.tapEvents removeObjectAtIndex:0]; // now calculate the average taps per second of the last three taps NSDate *start = self.tapEvents[0]; NSDate *end = [self.tapEvents lastObject]; self.averageTapsPerSecond = [self.tapEvents count] / [end timeIntervalSinceDate:start]; } - (void)startDisplayLink { self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; self.lastTime = CACurrentMediaTime(); [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } - (CGFloat)yAxisValueBasedUponTapsPerSecond { CGFloat y = 1024 - (self.averageTapsPerSecond * 100.0); return y; } - (void)handleDisplayLink:(CADisplayLink *)displayLink { CFTimeInterval now = CACurrentMediaTime(); CGFloat elapsed = now - self.lastTime; self.lastTime = now; if (elapsed &lt;= 0) return; CGPoint center = self.cider.center; CGFloat destinationY = [self yAxisValueBasedUponTapsPerSecond]; if (center.y == destinationY) { // we don't need to move it at all return; } else if (center.y &gt; destinationY) { // we need to move it up center.y -= self.speedInPointsPerSecond * elapsed; if (center.y &lt; destinationY) center.y = destinationY; } else { // we need to move it down center.y += self.speedInPointsPerSecond * elapsed; if (center.y &gt; destinationY) center.y = destinationY; } self.cider.center = center; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload