如何检查我的子视图是否被触摸?
我一直在尝试扩展教程,尝试让 Ryu 仅在被触摸时才会有动画。然而,触摸甚至没有被注册,我相信这与它是一个子视图有关。这是我的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if([touch view] == ryuView){
NSLog(@"Touch");
} else {
NSLog(@"No touch");
}
}
-(void) ryuAnims{
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
nil];
ryuView.animationImages = imageArray;
ryuView.animationDuration = 1.1;
[ryuView startAnimating];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
ryuView = image;
ryuView.image = [UIImage imageNamed:@"1.png"];
ryuView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:ryuView];
[image release];
}
该代码可以正常编译,但是,当触摸或单击 ryu 时,没有任何反应。我也尝试过,
if([touch view] == ryuView.image)
但这给了我这个错误:“不同 Objective-C 类型 'struct UIImage *' 和 'struct UIView *' 的比较缺少强制转换。”我做错了什么?
I've been attempting to expand on a tutorial by trying to make Ryu animate only when he is touched. However, the touch is not even being registered and I believe it has something to do with it being a subview. Here is my code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if([touch view] == ryuView){
NSLog(@"Touch");
} else {
NSLog(@"No touch");
}
}
-(void) ryuAnims{
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
nil];
ryuView.animationImages = imageArray;
ryuView.animationDuration = 1.1;
[ryuView startAnimating];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
ryuView = image;
ryuView.image = [UIImage imageNamed:@"1.png"];
ryuView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:ryuView];
[image release];
}
This code compiles fine, however, when touching or clicking on ryu, nothing happens. I've also tried
if([touch view] == ryuView.image)
but that gives me this error: "Comparison of distinct Objective-C type 'struct UIImage *' and 'struct UIView *' lacks a cast." What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIView
是否设置为接收触摸事件?Is the
UIView
set up to receive touch events at all?touchesBegan 是 UIView 类的方法,它应该在 UIView 的子类中实现(或者在你的 UIImageView 的子类中实现)案件)。不在你实现动画块的外部类中!
因此,请确保您创建 UIImageView 的子类,例如 RyuImageView 并实现 touchesBegan 方法。然后你应该用动画块通知你的对象 ryuView 被触摸了。您可以为此使用委托方法。请参阅答案 Xcode:如何在触摸图像(相同位置)时更改图像? 作为示例。
是的,将 UIImageView 的 userInteractionEnabled 设置为 YES 因为 UIImageView 的此属性的默认值为 NO
在 RyuImageView 的 initWith... 方法中执行此操作:
或者在你的动画块类中:
touchesBegan is method of UIView class, and it should be implemented in subclass of UIView (or of UIImageView in your case). Not in some outer class where you implement animation block!
So ensure that you create subclass of UIImageView e.g. RyuImageView and implement touchesBegan method. Then you should inform your object with animation block that ryuView was touched. You can use delegate method for this. See answer Xcode: How to change Image on Touching at an Image (same Position)? as sample.
And yes, set UIImageView's userInteractionEnabled to YES because default value of this property of UIImageView is NO
do it either in initWith... method of RyuImageView:
or in your animation block class: