【发布时间】:2010-06-03 18:53:08
【问题描述】:
所以我在按钮和动画方面遇到了问题。基本上,我正在使用 UIView 动画为视图制作动画,同时还尝试监听视图内按钮上的点击。视图和按钮一样大,视图实际上是 UIImageView 的子类,按钮下方有一个图像。该视图是放置在 Interface Builder 中的容器视图的子视图,其中启用了用户交互并启用了剪辑。所有的动画和按钮处理都在这个 UIImageView 子类中完成,而 startFloating 消息是根据需要从单独的类发送的。
如果我不做动画,buttonTapped: 消息会正确发送,但在动画期间不会发送。我也尝试过实现touchesEnded 方法,并且发生了相同的行为。
UIImageView 子类初始化(我将按钮填充了颜色,因此我可以看到框架设置正确,确实如此):
- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self != nil) {
// ...stuffs
UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tapBtn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[tapBtn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
tapBtn.backgroundColor = [UIColor cyanColor];
[self addSubview:tapBtn];
self.userInteractionEnabled = YES;
}
return self;
}
启动动画的动画方法(如果我不调用这个按钮可以正常工作):
- (void)startFloating {
[UIView beginAnimations:@"floating" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:10.0f];
self.frame = CGRectMake(self.frame.origin.x, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
所以,要清楚:
- 使用 UIView 动画可以有效地禁用按钮。
- 禁用动画会使按钮起作用。
- 按钮在屏幕上的大小和位置都正确,并且随着视图正确移动。
【问题讨论】:
标签: iphone cocoa-touch uiview uibutton