iOS:使用手势更改图像视图的 alpha

iOS: Change alpha of image view with gestures

我想用上下或左右等手势改变图像的透明度。 我不想为此使用滑块。

是否可以这样做,如果可以,请告诉我如何或者我必须为此使用滑块?

谢谢。

尝试如下操作:

首先附加点击 GestureRecognizer 以查看:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourTapMethod:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tap];

然后,在手势识别器处理程序中执行您想要的操作:

-(void)yourTapMethod{

[UIView animateWithDuration:0.5 animations:^(void){

     imageView.alpha = //add any value (like 0.2f);  
  }

您可以在视图或视图控制器上实现方法

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];

    NSLog(@"location: %@",NSStringFromCGPoint(location));

    // Example:
    // alpha depends on location.x related to view bounds width
    CGFloat alpha = (self.bounds.size.width - location.x) / self.bounds.size.width;
    NSLog(@"alpha: %@",@(alpha));
}

您可以在哪里查看 location.xlocation.y