gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:09:16 26 4
gpt4 key购买 nike

我有一个显示图片的 UIImageView。点击并按住时,只要用户将手指放在 ImageView 上,我就希望它变暗。基本上,我希望它像 UIButton 一样工作。

我目前使用 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 变暗?感谢您的帮助。

最佳答案

最简单的方法是在 QuartzCore 框架中使用层:

在您的项目设置中添加:QuartzCore.framework 到项目中添加到 .h 文件:#import

为长按创建手势,而不是像您描述的那样点击该行为:

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

关于ios - 如何在突出显示(点击)时使 UIImageView 变暗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com