ios - 使用 UIView 动画移动时的 xcode 点击按钮

标签 ios animation button action

我无法在按钮移动时执行按钮操作。 谁能帮我如何在点击时隐藏移动按钮? 当我点击按钮时它没有反应。 这是代码:

-(void)createTurtle
{

    NSUInteger r = arc4random_uniform(284) + 1;

    NSUInteger randomTitle = arc4random_uniform(1000000) + 1;

    turtle = [[UIButton alloc] init];
    turtle.frame = CGRectMake(r, 0, 36, 47);
    [turtle setImage:[UIImage imageNamed:@"turtle.png"] forState:UIControlStateNormal];
    [turtle addTarget:self action:@selector(turtleTouched:) forControlEvents:UIControlEventTouchDown];
    [turtle setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)randomTitle] forState:UIControlStateNormal];
    [turtle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:turtle];
    [self.view addSubview:turtle];



    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:16];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    turtle.frame = CGRectMake(turtle.frame.origin.x, self.view.frame.size.height, 36, 47);
    [UIView commitAnimations];


}







    - (void) turtleTouched: (id) sender
{
    UIButton *button = sender; // Typecast sender
    button.hidden = YES;
}

最佳答案

即使您点击它,您的方法也不会触发。但它会在您点击它的最后一帧时触发,同时设置动画。

假设你的按钮有 (0,0,100,100) 的初始帧,
现在你将它移动到 (200,200,100,100) 的帧。

在移动时,如果您在 (200,200,100,100)-final frame 区域单击,那么您将获得事件。但在 路径中间 的区域,例如 (50,50,100,100),您不会收到事件。

因为当您开始制作动画时,按钮的帧会立即变为最终帧,而按钮只是从初始位置过渡到最终位置。

因此,您可以重写 viewController 的 touchesBegan 方法并检查触摸点是否位于 transitional frame 中,而不是这样做。

transitionFrame = [button.layer.presentationLayer frame];

您的按钮的帧将始终是整个动画的最后一帧。

添加的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint p =[((UITouch *)[touches anyObject]) locationInView:self.view];
    CGRect r= [turtle.layer.presentationLayer frame];
    BOOL contains= CGRectContainsPoint(r, p);
    if(contains)
        turtle.hidden=YES;

}

关于ios - 使用 UIView 动画移动时的 xcode 点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21664043/

相关文章:

ios - 自动调整 UITableViewCell 变小

iphone - 如何在 Tableview 单元格 subview 项中分配 didSelectRowAtIndexPath

ios - 断断续续的全屏退出过渡

jquery - 使用 angular 动画化 CSS 属性

android - Phonegap 中的快速点击按钮

button - jqgrid EditActionIconsColumn 事件

c# - 按钮不能完全点击

ios - "Class not found, using default NSManagedObject"AppName 中有特殊字符

ios - 在启动时以编程方式为 5 个选项卡栏项目设置选项卡栏标题,其中 4 个嵌入在导航 Controller 中,1 个不是。 objective-c

iphone - 如何使旋转 UIView 的方法工作多次