Bhagyashree mahajan Bhagyashree mahajan - 4 months ago 22
Objective-C Question

iOS: Change alpha of image view with gestures

I want to change the alpha of image with the gestures like up and down or right and left.
I don't want to use slider for that.

Is it possible to do this if yes then please tell me how or i have to use slider for that?

Thank you.

Answer

You can implement method on view or view controller

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

where you can check location.x or location.y.

Comments