2011-12-09 67 views
0

我正在使用從屏幕頂部刪除UImages的降雪示例代碼。我想在UIImageview上檢測Touch並更新標籤。如果我在IBOutlet中創建單個UIImageView並將其連接到觸摸事件,則標籤會正確更新。但是當我嘗試將它應用於下降的UIImageView時,它不起作用。以下是我迄今爲止:UITouch動畫UIImage事件

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    score = 0; 
    self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0]; 
    flakeImage = [UIImage imageNamed:@"flake.png"]; 

    [NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 
} 

- (void)onTimer 
{ 

    flakeView = [[UIImageView alloc] initWithImage:flakeImage]; 
    flakeView.opaque = YES; 
    flakeView.userInteractionEnabled = YES; 
    flakeView.multipleTouchEnabled = YES; 

    int startX = round(random() % 320); 
    int endX = round(random() % 320); 

    double scale = 1/round(random() % 100) + 1.0; 
    double speed = 1/round(random() % 100) + 1.0; 

    flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale); 

    [self.view addSubview:flakeView]; 
    [self.view bringSubviewToFront:flakeView]; 

    [UIView beginAnimations:nil context:flakeView]; 
    [UIView setAnimationDuration:5 * speed]; 

    flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale); 

    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)]; 
    [UIView setAnimationDelegate:self]; 
    [UIView commitAnimations];  
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    if ([touch view] == flakeView) 
    { 
     NSLog(@"tag %@",touch); 
     score = score + 1; 
     lbl1.text = [NSString stringWithFormat:@"%i", score];   
    } 
} 
+0

這裏是你的問題的解決方案.. http://stackoverflow.com/questions/6852020/typing-while-animation-uitextview/6852092#6852092 –

回答

1

動畫通常禁用觸摸操作,所以你必須使用:

animateWithDuration:delay:options:animations:completion 

設置選項,包括接收觸摸。

http://developer.apple.com/library/IOs/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW1

IIRC你想要的選項:UIViewAnimationOptionAllowUserInteraction在完整列表上面的文檔查找UIViewAnimationOptions。

+1

[UIView的animateWithDuration:0.1延時:0.0選項: UIViewAnimationOptionAllowUserInteraction動畫:^ {UIView beginAnimations:nil context:flakeView]; [UIView setAnimationDuration:5 * speed]; flakeView.frame = CGRectMake(endX,500.0,25.0 * scale,25.0 * scale); [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context :)]; [UIView setAnimationDelegate:self]; [UIView commitAnimations];} completion:nil]; – user865149

+0

我已經嘗試過這一點,並把這個在我添加flakeView作爲子視圖後。但仍然沒有成功。 – user865149

+0

動畫塊中的beginAnimations可能是問題所在。您希望允許交互的每個動畫都必須指定UIViewAnimationOptionAllowUserInteraction。 –