Bruno
Bruno

Reputation: 1032

Touch position highlight

I'm trying to highlight a touch event by using UIGestureRecognizer, so far I can get the CGPoint where the user touched but I don't know how to highlight that specific area since I don't know much about animation.

The code so far on viewDidLoad:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gradientTouchDown:)];

[self addGestureRecognizer:singleFingerTap];

The method that gets the position of the touch:

-(void)gradientTouchDown:(UIGestureRecognizer*)recognizer{
    NSLog(@"tap detected.");
    CGPoint point = [recognizer locationInView:nil];

    NSLog(@"x = %f y = %f", point.x, point.y );

}

By doing some study I think I need to add the code above with a View animation, but how can I recognize the touch? the code:

  [UIView animateWithDuration:0.3f
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^
     {
         [sender setAlpha:0.3f];
     }
                     completion:^(BOOL finished)
     {
         [sender setAlpha:1.0f];
     }];

I really need help on this one, any question just ask, I'm always around here :)

Upvotes: 0

Views: 576

Answers (2)

Bruno
Bruno

Reputation: 1032

Hi and thanks for the help, I came out with a custom solution for a similar glow effect related to the Apple "Shows Touch on Highlight" here is the code for the glow:

-(void)gradientTouchDown:(UIGestureRecognizer*)recognizer{
    NSLog(@"tap detected");
    CGPoint point = [recognizer locationInView:nil];


    NSLog(@"x = %f y = %f", point.x, point.y );


    UIImage* image;
    UIColor *color = [UIColor whiteColor];

    UIGraphicsBeginImageContextWithOptions(recognizer.view.bounds.size, NO, [UIScreen mainScreen].scale); {
        [recognizer.view.layer renderInContext:UIGraphicsGetCurrentContext()];

        UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, recognizer.view.bounds.size.width, recognizer.view.bounds.size.height)];

        [color setFill];

        [path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];


        image = UIGraphicsGetImageFromCurrentImageContext();
    } UIGraphicsEndImageContext();

    UIView* glowView = [[UIImageView alloc] initWithImage:image];
    glowView.center = self.center;
    [self.superview insertSubview:glowView aboveSubview:self];

    glowView.alpha = 0;
    glowView.layer.shadowColor = color.CGColor;
    glowView.layer.shadowOffset = CGSizeZero;
    glowView.layer.shadowRadius = 10;
    glowView.layer.shadowOpacity = 1.0;


    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = @(0.0f);
    animation.toValue = @(0.6f);
    //animation.repeatCount = 2 ? HUGE_VAL : 0;
    animation.duration = 0.2;
    animation.autoreverses = YES;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [glowView.layer addAnimation:animation forKey:@"pulse"];


}

Any suggestion for improvement is welcome, so far it works for me :)

Upvotes: 0

AC1
AC1

Reputation: 463

This is how I implemented my animation in one of my projects for a button. The animation just puts a glow around the button when pressed (just like the glow you see when you press an year button on the graph view of built in stocks app)

First, add these targets to the button:

[button addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(myButtonReleased:) forControlEvents:UIControlEventTouchUpInside];

Then these methods looks like this:

- (void) myButtonPressed:(id) sender
{
    UIButton *button = (UIButton *) sender;
    CABasicAnimation *shadowRadius = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ];
    shadowRadius.delegate = self;
    [shadowRadius setFromValue:[NSNumber numberWithFloat:3.0]];
    [shadowRadius setToValue:[NSNumber numberWithFloat:15.0]];
    [shadowRadius setDuration:0.2f];

    [[button layer] addAnimation:shadowRadius forKey:@"shadowRadius"];

    button.layer.shadowRadius = 15.0f;
}
- (void) myButtonReleased:(id) sender
{
    UIButton *button = (UIButton *) sender;

    CABasicAnimation *shadowRadius = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ];
    shadowRadius.delegate = self;
    [shadowRadius setFromValue:[NSNumber numberWithFloat:15.0]];
    [shadowRadius setToValue:[NSNumber numberWithFloat:3.0]];
    [shadowRadius setDuration:0.2f];
    //shadowRadius.autoreverses = YES;

    [[button layer] addAnimation:shadowRadius forKey:@"shadowRadius"];

    button.layer.shadowRadius = 3.0f;
    button.selected = YES;
}

Upvotes: 1

Related Questions