2013-05-12 79 views
5

我有一個顯示圖片的UIImageView。點擊並按住時,只要用戶將手指放在圖像視圖上,就會使其變暗。基本上,我希望它像UIButton一樣行事。突出顯示(點擊)時,如何使UIImageView變暗?

我現在已經將其與UIGestureRecognizer做到這一點:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
             initWithTarget:self 
             action:@selector(pictureViewTapped)]; 
    singleTap.numberOfTapsRequired = 1; 
    singleTap.numberOfTouchesRequired = 1; 
    [self.pictureImageView addGestureRecognizer:singleTap]; 
    [self.pictureImageView setUserInteractionEnabled:YES]; 
} 

,並在我的pictureViewTapped,我(現在)有這樣的:

- (void)pictureViewTapped { 
    NSLog(@"Picture view was tapped!"); 
} 

如何使暗的UIImageView?任何幫助表示讚賞。

+1

如果您想表現得像一個按鈕,爲什麼不使用按鈕? – danypata 2013-05-12 18:49:13

回答

15

最簡單的方法是在QuartzCore框架使用層:

附加:QuartzCore.framework在您的項目設置項目 添加到.h文件中:#進口

爲長按,而不是創造的姿態如你描述抽頭作爲行爲:稱爲

self.longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(darkenImage)]; 
    [self.imageView setUserInteractionEnabled:YES]; 
    [self.imageView addGestureRecognizer:self.longTap]; 

方法當手勢活性:

-(void)darkenImage { 
    switch (self.longTap.state) { 
     case UIGestureRecognizerStateBegan: // object pressed 
     case UIGestureRecognizerStateChanged: 
      [self.imageView.layer setBackgroundColor:[UIColor blackColor].CGColor]; 
      [self.imageView.layer setOpacity:0.9]; 
      break; 
     case UIGestureRecognizerStateEnded: // object released 
      [self.imageView.layer setOpacity:1.0]; 
      break; 
     default: // unknown tap 
      NSLog(@"%i", self.longTap.state); 
      break; 
    } 
} 
+0

工作,謝謝! – swiftcode 2013-05-13 10:39:11

+7

'[self.imageView.layer setBackgroundColor:[UIColor blackColor] .CGColor]; [self.imageView.layer setOpacity:0.9];' 是意圖使背景將顯示通過當您將整個imageViews不透明度爲0.9?背景是否應該不受不透明度變化的影響?總之,這不適合我。 :( – Toydor 2014-12-17 11:53:59

+0

@Toydor我同意,我建議未來的讀者嘗試使用'.tintColor'屬性代替。 – 2018-02-16 00:44:22

0

斯威夫特 - 解決方案:

@IBOutlet weak var imageViewExport: UIImageView! 
var tapGestureExport = UILongPressGestureRecognizer() 

override func viewDidLoad() { 
     super.viewDidLoad() 

     imageViewExport.userInteractionEnabled = true 

     tapGestureExport = UILongPressGestureRecognizer(target: self, action: "tapGestureExportAction:") 
     imageViewExport.addGestureRecognizer(tapGestureExport) 
    } 

    func tapGestureExportAction(sender: UITapGestureRecognizer) { 

     switch(tapGestureExport.state) { 
     case UIGestureRecognizerState.Began, 
       UIGestureRecognizerState.Changed: 
      imageViewExport.backgroundColor = UIColor.blackColor() 
      imageViewExport.layer.opacity = 0.9 

     case UIGestureRecognizerState.Ended:  // released 
      imageViewExport.layer.opacity = 1.0 

      default: break // unknown tap 
      } 
} 
0

在我的情況下,設置的ImageView圖層不透明度改爲0.9時,背景將顯示出來。

我使用使用新圖層的解決方案。

let coverLayer = CALayer() 
coverLayer.frame = imageView.bounds; 
coverLayer.backgroundColor = UIColor.blackColor().CGColor 
coverLayer.opacity = 0.0 
imageView.layer.addSublayer(coverLayer) 

// when tapped 
coverLayer.opacity = 0.1