gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:30:58 28 4
gpt4 key购买 nike

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

-(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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com