2012-01-05 107 views
12

我有一個UIView與一些子視圖和一個點擊手勢識別相關聯,我想模仿它有'觸摸'的影響。也就是說,當輕敲發生時,我想顯示容器視圖具有不同的背景顏色,並且任何子視圖UILabels的文本也顯示爲突出顯示。突出顯示UIView類似於UIButton

當我收到來自UITapGestureRecognizer水龍頭事件,我可以改變背景顏色就好了,甚至設置的UILabel到[label setHighlighted:YES];

由於種種原因,我不能UIView的改變UIControl。

但是,如果我添加一些UIViewAnimation來恢復突出顯示,沒有任何反應。有什麼建議麼?

- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture { 
     [label setHighlighted:YES]; // change the label highlight property 

[UIView animateWithDuration:0.20 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{ 
         [containerView setBackgroundColor:originalBgColor];   
         [label setHighlighted:NO]; // Problem: don't see the highlight reverted 
        } completion:^(BOOL finished) {       
         // nothing to handle here 
        }];  
} 
+0

爲什麼不讓它'UIButton'? – 2012-01-05 17:40:12

+0

因爲它不是我擁有的代碼庫,還有其他的依賴關係,所以我必須將它作爲UIView。 – 2012-01-05 17:41:04

+0

看看這個庫:https://github.com/mta452/UIView-TouchHighlighting – 2016-07-23 16:21:25

回答

6

setHighlighted不是一個動畫視圖屬性。另外,你說的是兩個相反的東西:你把同樣的氣息強調爲YES和NO。結果將是沒有發生任何事情,因爲沒有整體變化。

使用完成處理程序或延遲性能更改高亮後面

編輯:

你說「兩個都試過但都沒有工作。」也許你需要澄清我的意思是延遲的表現。我只是想這和它完美的作品:

- (void) tapped: (UIGestureRecognizer*) g { 
    label.highlighted = YES; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     label.highlighted = NO; 
    }); 
} 

的標籤必須有不同的textColor VS其highlightedTextColor使事情發生可見。

+0

雖然都嘗試過,但都沒有工作..我可能不得不找出另一種創造性的方式來做到這一點,或去重新佈線現有的代碼庫使用UIControl。 – 2012-01-05 17:54:54

0

簡單的解決方案是重寫雙擊手勢regognizer 象下面這樣:

斯威夫特4.x的

class TapGestureRecognizer: UITapGestureRecognizer { 
    var highlightOnTouch = true 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { 
     super.touchesBegan(touches, with: event) 

     if highlightOnTouch { 
      let bgcolor = view?.backgroundColor 
      UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
       self.view?.backgroundColor = .lightGray 
      }) { (_) in 
       UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: { 
        self.view?.backgroundColor = bgcolor 
       }) 
      } 
     } 
    } 

} 
相關問題