简体   繁体   English

突出显示(点击)时如何使UIImageView变暗?

[英]How do I make a UIImageView darker when highlighted (tapped)?

I have a UIImageView that shows a picture. 我有一个显示图片的UIImageView。 When tapped and held, I'd like it to darken as long as the user has their finger over the imageview. 当点击并保持时,只要用户将手指放在图像视图上,我就希望它变暗。 Basically, I want it to act like a UIButton. 基本上,我希望它像UIButton一样。

I currently have it do this with a UIGestureRecognizer: 我目前使用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];
}

and in my pictureViewTapped , I have this (for now): 在我的pictureViewTapped ,我有这个(现在):

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

How do I make the UIImageView darker? 如何使UIImageView变暗? Any help is appreciated. 任何帮助表示赞赏。

Easiest way is to use layers in the QuartzCore framework: 最简单的方法是在QuartzCore框架中使用图层:

add: QuartzCore.framework to project in your project settings add to .h file: #import add:在项目设置中项目的QuartzCore.framework添加到.h文件:#import

create gesture for long press instead of tap as that behaviors as you described: 为长按创建手势而不是按照您描述的行为进行点击:

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

method called when gesture active: 手势激活时调用的方法:

-(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;
    }
}

In my case, background will show through when set ImageView's layer opacity to 0.9. 在我的例子中,当将ImageView的图层不透明度设置为0.9时,将显示背景。

I use solution that use new layer. 我使用使用新图层的解决方案。

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

Swift-Solution: 斯威夫特 - 解决方案:

@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 
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM