0

我正在尝试使用 突出显示触摸事件UIGestureRecognizer,到目前为止,我可以获得CGPoint用户触摸的位置,但我不知道如何突出显示该特定区域,因为我对动画知之甚少。

到目前为止的代码viewDidLoad

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

[self addGestureRecognizer:singleFingerTap];

获取触摸位置的方法:

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

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

}

通过做一些研究,我认为我需要在上面的代码中添加一个 View 动画,但是我怎样才能识别触摸呢?编码:

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

我真的需要这方面的帮助,任何问题都可以问,我总是在这里:)

4

2 回答 2

1

这就是我在我的一个按钮项目中实现动画的方式。按下时,动画只会在按钮周围发光(就像您在内置股票应用程序的图表视图上按下年份按钮时看到的发光一样)

首先,将这些目标添加到按钮中:

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

那么这些方法看起来像这样:

- (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;
}
于 2013-10-03T17:20:05.457 回答
0

嗨,感谢您的帮助,我提出了一个自定义解决方案,用于与 Apple“高亮显示触摸”相关的类似发光效果,这里是发光的代码:

-(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"];


}

欢迎任何改进建议,到目前为止它对我有用:)

于 2013-10-04T14:31:49.220 回答