Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the current position of UIView during animation

I am trying to find the current position of an iPhone ImageView that is animating across the screen.

I create and animate the imageView like so

-(IBAction)button:(UIButton *)sender{     UIImageView *myImageView =[[UIImageView alloc] initWithImage:                                      [UIImage imageNamed:@"ball.png"]];         myImageView.frame = CGRectMake(0, self.view.frame.size.height/2, 40, 40);         [self.view addSubview:myImageView];     [myArray addObject:myImageView];      [UIView animateWithDuration:.5                            delay:0                  options:(UIViewAnimationOptionAllowUserInteraction) // | something here?)                      animations:^{                          myImageView.frame = CGRectOffset(myImageView.frame, 500, 0);                          }                      completion:^(BOOL finished){                          [myArray removeObject:myImageView];                          [myImageView removeFromSuperview];                          [myImageView release];                      }      ]; } 

then to detect the current CGPoint of the imageView I call this method every 60th of a second

-(void)findPoint{      for (UIImageView *projectile in bullets) {                  label.text = [NSString stringWithFormat:@"%f", projectile.center.x];          label2.text = [NSString stringWithFormat:@"%f", projectile.center.y];     } } 

However this only gives me the point where the animation ends, I want to get the current CGPoint of the image that is animating across the screen. Is there an option that I need to apply other than UIViewAnimationOptionAllowUserInteraction to do this? If not, how do I get the current position of an animating imageView?

like image 278
user975134 Avatar asked Oct 02 '11 05:10

user975134


People also ask

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Is UIView animate asynchronous?

UIView. animate runs on the main thread and is asynchronous.


2 Answers

You can use the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed". Just use it like this:

CGRect projectileFrame = [[projectile.layer presentationLayer] frame]; 
like image 86
adamsiton Avatar answered Nov 05 '22 02:11

adamsiton


Swift 5: Get temporary position and size (CGRect) during the animation with:

let currentFrame = projectileFrame.layer.presentation()!.frame 
like image 29
user2888102 Avatar answered Nov 05 '22 00:11

user2888102