简体   繁体   中英

How to get UIButton position during animation

This is my code

- (void)viewDidLoad {
[super viewDidLoad];
self.timer = [NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(creatButton) userInfo:nil repeats:YES];

}

- (NSMutableArray *)buttonsArray {
if (!_buttonsArray) {
    _buttonsArray = [[NSMutableArray alloc] init];
}
return _buttonsArray;

}

- (void)creatButton {
NSInteger j = arc4random() % 250;
UIButton *countButton = [[UIButton alloc] initWithFrame:CGRectMake((j * 20) % ((NSInteger)self.view.frame.size.width - 120), -300, 100, 50)];

[self.buttonsArray addObject:countButton];

[self.view addSubview:countButton];

[UIView animateWithDuration:9 delay:0.f
                    options:(UIViewAnimationOptionCurveLinear)
                    animations:^{

                        countButton.center = CGPointMake(50 + (j * 20) % ((NSInteger)self.view.frame.size.width - 120), self.view.frame.size.height);
                    }
                    completion:nil];

}

and I want to know has any way to get countButton 's position while the animation is "in-flight".

The way view animation is configured in UIKit/CoreAnimation, as soon as an animation block is called, the view's properties are set to the final value, so reading counterButton.center will only return the final value. Similarly, the layer's properties are probably pinned to the final value (though not strictly by necessity).

What you can do is use the view's layer's presentation layer, [countButton.layer presentationLayer] , to get an approximate snapshot of the layer's properties as they appear on screen.

From Apple, emphasis added :

Declaration

 - (instancetype)presentationLayer; 

Return Value

A copy of the current presentation layer object.

Discussion

The layer object returned by this method provides a close approximation of the layer that is currently being displayed onscreen . While an animation is in progress, you can retrieve this object and use it to get the current values for those animations.

By default, a view's center is the same as view.layer.position , so you could get the button's in-animation center with:

[countButton.layer presentationLayer].position

James Baxter's answer is the right way to go. You need to query the presentationLayer that's being animated.

Note that UIView animations use CAAnimations under the covers, so it's possible to interrogate the position of a view that's being animated by a UIView animation the same way.

I have a demo project on Github called iOS-CAAnimation-group-demo that demonstrates a number of techniques including "hit testing" in-flight UIView and CAAnimation animations so you can click on an image that's animating to pause/resume the animation. It's written in Objective-C, but it looks like that's what you want, so it should b helpful.

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