【发布时间】:2012-05-14 12:52:35
【问题描述】:
我有一个UIImageView,它在动画循环中。我想检测它是否被触摸并打印出带有NSLog 的消息。如果它被触摸,这个想法将是执行另一个动画,但目前我无法检测到它是否已经被触摸。已启用用户交互。代码如下:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
if (touchedView == imgSun) {
NSLog(@"Sun touched");
[self spinSun];
} else if (touchedView == imgBee) {
NSLog(@"Bee touched");
} else if (touchedView == imgClouds) {
NSLog(@"Clouds touched");
}
}
动画方法:
-(void) beeBobbing
{
[UIView animateWithDuration:1.0f delay:0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
CGPoint bottomPoint = CGPointMake(215.0, 380.0);
imgBee.center = bottomPoint;
} completion:^(BOOL finished) {
}];
}
【问题讨论】:
-
你是在继承 UIImageView 吗?因为否则您无法访问 touchesBegan 委托方法。另外,你为什么不简单地使用 UIGestureRecongizers?
-
另一种方法是使用自定义按钮并将图像作为该按钮的背景图像,然后您可以轻松理解是否触摸它
-
我对编程很陌生,之前没有使用过 UIGestureRecognizers。在开始使用触摸之前,我决定这样做。以下答案似乎对我有用
标签: iphone ios uiimageview touchesbegan