2014-05-08 74 views
0

如果我在setHighlighted:方法中爲其設置動畫,則在連續多次觸摸自定義按鈕時遇到問題。基本上我想改變觸摸按鈕的背景顏色。連續多次突出顯示動畫的觸摸按鈕

-(void)setHighlighted:(BOOL)highlighted 
{ 
    [super setHighlighted:highlighted]; 

    if(highlighted) 
    { 
     [UIView animateWithDuration:0.1 
       delay:0.0 
       options:UIViewAnimationOptionAllowUserInteraction 
       animations:^{ 
        self.backgroundColor = [UIColor darkGrayColor]; 
     } completion:^(BOOL finished) {}]; 
    } 
    else 
    { 
     [UIView animateWithDuration:0.1 animations:^{ 
      self.backgroundColor = [UIColor clearColor]; 
     } completion:^(BOOL finished) {}]; 
    } 

問題是我必須等到動畫完成。在動畫過程中,不調用按鈕的目標動作。 我該如何處理這些多點觸摸?

+1

也許通過使用'addTarget:'方法爲您的UIButton,你觸發其改變的UIButton的背景顏色選擇。我認爲你不必等待再次按下按鈕並改變顏色。 –

回答

1

嘗試增加delay:0.0 options:UIViewAnimationOptionAllowUserInteraction到其他animateWithDuration:塊。

代碼:或者,您可以通過這兩個動畫塊這樣的組合簡化代碼

-(void)setHighlighted:(BOOL)highlighted 
{ 
    [super setHighlighted:highlighted]; 

    UIColor *colorToChangeTo = highlighted ? [UIColor darkGrayColor] : [UIColor clearColor]; 

    [UIView animateWithDuration:0.1 
          delay:0.0 
         options:UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         self.backgroundColor = colorToChangeTo; 
        } completion:^(BOOL finished) {}]; 
} 
+1

我怎麼能這麼愚蠢......非常感謝@DrBeardface – crazykenny

0

您可以將深灰色的圖像設置爲突出顯示按鈕的圖像。

更新

雖然我還是會建議與高亮顯示的圖像去,這裏是一個替代的解決方案。

- (void)setHighlighted:(BOOL)highlighted 
{ 
    [UIView transitionWithView:self 
         duration:0.3f 
         options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         self.backgroundColor = highlighted ? [UIColor darkGrayColor] : [UIColor clearColor]; 
         [super setHighlighted:highlighted]; 
        } completion:nil]; 
} 
+0

通過設置突出顯示的圖像,我將不會獲得動畫。 – crazykenny

+0

@crazykenny實際上,當您設置突出顯示的圖像時,可能會有0.1秒的動畫動畫。但無論如何,我已經更新了我的答案。這對我有效。 – Adithya

0

您的.h文件中 布爾ISCAN;

.m文件

- (void)viewDidLoad 
    { 
     isCan=YES; 
     [super viewDidLoad]; 
     NSArray *temp=[NSArray arrayWithObjects:@"button1",nil]; 
     for (int i=0; i<=0; i++) 
     { 
      _likeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [_likeBtn setFrame:CGRectMake(20+(i*80), 220, 102, 20)]; 
      [_likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
      [_likeBtn setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal]; 
      [_likeBtn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
      [_likeBtn setTag:i]; 
      [self.view addSubview:_likeBtn]; 
     } 

     // Do any additional setup after loading the view from its nib. 
    } 

    -(void)setHighlighted:(BOOL)highlighted 
    { 

     if(highlighted) 
     { 
      [UIView animateWithDuration:0.1 
            delay:0.0 
           options:UIViewAnimationOptionAllowUserInteraction 
          animations:^{ 
           self.likeBtn.backgroundColor = [UIColor blackColor]; 
          } completion:^(BOOL finished) {}]; 
     } 
     else 
     { 
      [UIView animateWithDuration:0.1 animations:^{ 
       self.likeBtn.backgroundColor = [UIColor redColor]; 
      } completion:^(BOOL finished) {}]; 
     } 
    } 
    - (IBAction)action:(id)sender 

    { 
     if (isCan) 
     { 
      [self setHighlighted:YES]; 
      isCan=NO; 
     } 
    else 
    { 
     [self setHighlighted:NO]; 
isCan=YES; 
    } 
    }