0

我一直试图通过尝试让 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 *'的比较缺少演员表。” 我究竟做错了什么?

4

2 回答 2

1

是否UIView设置为接收触摸事件?

ryuView.userInteractionEnabled = YES;
于 2010-03-31T21:44:25.530 回答
0

touchesBeganUIView类的方法,它应该在UIView的子类(或你的情况下的UIImageView)中实现。不在您实现动画块的外部类中!

因此,请确保创建UIImageView的子类,例如RyuImageView并实现touchesBegan方法。然后你应该用动画块通知你的对象 ryuView 被触摸了。您可以为此使用委托方法。查看答案 Xcode: How to change Image on Touching at an Image (same Position)? 作为样本。

是的,将 UIImageView 的 userInteractionEnabled设置为YES因为 UIImageView 的这个属性的默认值是NO

在 RyuImageView 的 initWith... 方法中执行此操作:

self.userInteractionEnabled = YES;

或在您的动画块类中:

 ryuView.userInteractionEnabled = YES;
于 2010-03-31T22:23:30.440 回答