这是我的代码:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:100];
[UIView setAnimationRepeatCount:1000];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationBeginsFromCurrentState:YES];

CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
CGFloat y = (CGFloat) (arc4random() % (int) 463);

if(y < 90)
    y = 90;

if(x > 270)
    x = 270;

CGPoint squarePostion = CGPointMake(x, y);

button.center = squarePostion;

[UIView setAnimationDelegate:self];
[UIView commitAnimations];


我了解我需要在某个地方添加“ UIViewAnimationOptionAllowUserInteraction”,但是我该怎么做呢?

最佳答案

这在动画期间对我有用。希望也能为您服务。
在动画过程中将按钮对象添加到数组中,并在动画完成时将其从数组中删除。

[self.arrBtn addObject:btn];

[self.view addSubview:btn];
[self.view bringSubviewToFront:btn];

[UIView animateWithDuration:8.0f
                      delay:0.0f
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     btn.frame = CGRectMake(endX, 500.0, 50.0 * scale, 50.0 * scale);
                 }
                 completion:^(BOOL finished){

                     [self.arrBtn removeObject:btn];

                 }];


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.arrBtn) // arrBtn is array of button
{
    if ([button.layer.presentationLayer hitTest:touchLocation])
    {
        // This button was hit whilst moving - do something with it here

        NSLog(@"Button Clicked");
        break;
    }
}
}

关于ios - 如何在动画中移动UIButton时进行触摸?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25162884/

10-10 20:35