Iphone 触摸位置突出显示
我正试图通过使用Iphone 触摸位置突出显示,iphone,ios,objective-c,cocoa-touch,Iphone,Ios,Objective C,Cocoa Touch,我正试图通过使用uigestureerecognizer突出显示触摸事件,到目前为止,我可以获得用户触摸的CGPoint,但我不知道如何突出显示该特定区域,因为我对动画知之甚少 到目前为止,viewDidLoad上的代码: UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gradientTouchDown:)]; [sel
uigestureerecognizer
突出显示触摸事件,到目前为止,我可以获得用户触摸的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 );
}
通过做一些研究,我想我需要添加一个视图动画上面的代码,但我如何才能识别触摸?守则:
[UIView animateWithDuration:0.3f
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^
{
[sender setAlpha:0.3f];
}
completion:^(BOOL finished)
{
[sender setAlpha:1.0f];
}];
我真的需要这方面的帮助,只要问任何问题,我总是在这里:)这就是我如何在我的一个项目中为一个按钮实现我的动画。当按下按钮时,动画只会在按钮周围发光(就像在内置股票应用程序的图形视图中按下年份按钮时看到的发光) 首先,将这些目标添加到按钮:
[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;
}
您好,感谢您的帮助,我推出了一个自定义解决方案,用于类似于Apple“Shows Touch on Highlight”的辉光效果。以下是辉光的代码:
-(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"];
}
欢迎提出任何改进建议,到目前为止它对我有效:)您是否尝试过创建视图、设置其帧、使用块动画淡入淡出并将其从superview中删除?这里是一个很好的起点(而不是在StackOverflow上问无意义的开放式问题):你能告诉我你想要什么样的动画吗?只是用户点击的一个点或形状,或者用户平移时的一个路径?我使用了如下内容:[UIView animateWithDuration:0.3f delay:0.0选项:UIViewAnimationOptionCurveEaseIn animations:^{[sender setAlpha:0.3f];}完成:^(BOOL finished){[sender setAlpha:1.0f];}];动画很简单,只是在触摸时突出显示:)