简体   繁体   中英

get coordinates of animated UIImageview

I am animating an UIImageview in horizontal position for this purpose i have used the below code i have used the NSTimer

timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

-(void)onTimer
{
    [UIView animateWithDuration:10.0f animations:^{
          //Moving_Cloud is an image view
            Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
           }];

}

now the problem i am facing is i need to get the coordinates of the "Moving_Cloud" after the animate duration
please help me
Thanks in advance.

[UIView animateWithDuration:10.0f animations:^{
    Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
} completion:^(BOOL finished){
    CGPoint newPost = Moving_Cloud.frame.origin;
    CGFloat xPos = newPost.x;
    CGFloat yPos = newPost.y;
    // do stuff ?
}];

At the end of your animation the block "completion:" will be trigger.
Normally your image should be in x = 200, y = 150.

Be aware that those coordinates are relative to it superview (the view wrapping Moving_Cloud view).

Note:
By convention I recommend changing "Moving_Cloud" to "movingCloud".
Instance class start in lower cap in objective-C.
Also don't use _ but a capital letter instead.

Abhijit Chaudhari from the comment in my previous post I understand that what you want is not the position "after the animate duration" but instead the position during the animation.

If I still didn't get it right please clarify your question. (Or buy me an new brain)

-(void) animateMe(){
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    [UIView animateWithDuration:10.0f animations:^{
         //Moving_Cloud is an image view
         Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
    }];
}

-(void)onTimer:(id)sender{
    NSLog(@"Currently in x=%f", [[ddddd.layer presentationLayer] frame].origin.x);
    NSLog(@"Currently in y=%f", [[ddddd.layer presentationLayer] frame].origin.y);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM