2010-03-31 73 views
0

我一直在試圖通過努力讓劉某動畫,只有當他被觸摸到的教程擴展。然而,觸摸甚至沒有被註冊,我相信它與它是一個子視圖有關。這裏是我的代碼:如何檢查我的子視圖是否被觸摸?

-(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的比較式‘結構的UIImage *’和‘結構的UIView *’缺乏投」我究竟做錯了什麼?

回答

1

是在UIView設置爲接收所有觸摸事件?

ryuView.userInteractionEnabled = YES; 
0

的touchesBegan的UIView類的方法,它應該在的UIView(或的UIImageView你的情況)的子類實現。不在某些實現動畫塊的外部類中!

所以請確保您創建的子類的UIImageView例如RyuImageView和實施的touchesBegan方法。然後你應該通過動畫塊告知你的對象ryuView被觸及。你可以使用委託方法。請參閱答案Xcode: How to change Image on Touching at an Image (same Position)?作爲示例。

是的,設置的UIImageView的userInteractionEnabledYES因爲的UIImageView的這個屬性的默認值是NO

做到這一點無論是在initWith ... RyuImageView的方法:

self.userInteractionEnabled = YES; 

或在你的動畫塊類:

ryuView.userInteractionEnabled = YES; 
相關問題