触摸位置突出显示

时间:2013-10-03 16:03:03

标签: iphone ios objective-c cocoa-touch

我正在尝试使用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];
     }];

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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"];


}

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