2014-02-09 110 views
3

當按鈕移動時,我無法進行按鈕操作。 任何人都可以幫助我如何在點擊時隱藏移動按鈕? 當我點擊按鈕時沒有反應。 下面的代碼:如果你想隱藏它的任何觸摸事件使用UIViewAnimation移動時xcode tap按鈕

- (void) turtleTouched: (id) sender 
{ 
    UIButton *button = sender; // Typecast sender 
    button.hidden = YES; 
} 

,使用以下命令::

-(void)createTurtle 
{ 

    NSUInteger r = arc4random_uniform(284) + 1; 

    NSUInteger randomTitle = arc4random_uniform(1000000) + 1; 

    turtle = [[UIButton alloc] init]; 
    turtle.frame = CGRectMake(r, 0, 36, 47); 
    [turtle setImage:[UIImage imageNamed:@"turtle.png"] forState:UIControlStateNormal]; 
    [turtle addTarget:self action:@selector(turtleTouched:) forControlEvents:UIControlEventTouchDown]; 
    [turtle setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)randomTitle] forState:UIControlStateNormal]; 
    [turtle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [self.view bringSubviewToFront:turtle]; 
    [self.view addSubview:turtle]; 



    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:16]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

    turtle.frame = CGRectMake(turtle.frame.origin.x, self.view.frame.size.height, 36, 47); 
    [UIView commitAnimations]; 


} 







    - (void) turtleTouched: (id) sender 
{ 
    UIButton *button = sender; // Typecast sender 
    button.hidden = YES; 
} 

回答

1

你的方法不會即使點擊它也會發生火災。但是它在動畫的同時也是will fire when you click in its final frame

比方說,你按鈕具有的(0,0,100,100) inital框架,
現在你將它移動到的(200,200,100,100幀)。

移動時,如果您點擊(200,200,100,100)-final frame的區域,那麼您將獲得事件。但在middle of path的區域,如(50,50,100,100),您將不會收到活動。

因爲當你開始動畫時,按鈕的框架會立即變成最終框架,而按鈕只是從初始位置轉換到最終位置。

因此,您可以替代您的viewController的touchesBegan方法,並檢查觸點是否位於transitional frame中。

transitionFrame = [button.layer.presentationLayer frame]; 

你的按鈕的框架將永遠是整個動畫的最後一幀。

代碼添加:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CGPoint p =[((UITouch *)[touches anyObject]) locationInView:self.view]; 
    CGRect r= [turtle.layer.presentationLayer frame]; 
    BOOL contains= CGRectContainsPoint(r, p); 
    if(contains) 
     turtle.hidden=YES; 

} 
+0

感謝你的答案工作!謝謝! –

+0

這是它!非常感謝你!它工作得很好! –

1

試試這個代碼

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     buttonNamr.hidden =YES; 
} 
+0

感謝您的幫助,但它仍然無法正常工作。是否有另一個代碼: - (void)touchesBegan:(NSSet *)與事件觸發:(UIEvent *)事件 ? –

+0

如果你正在使用按鈕,它應該工作 –

+0

你能告訴我你的確切要求/ –