我有一个自定义的 UIView
,我想在其上显示简单的动画形状以响应触摸。例如:
- 每当用户点击屏幕时,手指下方会出现一个带有动画的圆圈
- 如果用户结束触摸,圆圈应该随着动画消失
- 如果用户到达屏幕的某个部分,圆圈应该(平滑地)增加其大小
我现在正在做的(请参阅下面的代码)是使用 touchesBegan
、touchesMoved
等来设置所有触摸.然后在 drawRect
中,我为每个触摸绘制一个圆圈。一切都是超静态的。
制作动画的最佳方式是什么?
谢谢!
PS:为了完整起见,这里是我代码的一部分
- (void)drawRect:(CGRect)rect
{
for (UITouch *touch in ts) { //ts is the set with all touches
CGPoint location = [touch locationInView:touch.view];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectangle = CGRectMake(location.x-circleSize/2, location.y-circleSize/2+correctionY,
circleSize*1.15, circleSize*1.15);
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(context, rectangle);
/* etc etc */
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
/* etc etc */
ts = [[NSMutableSet setWithSet: [event touchesForView:self]] retain];
/* etc etc */
}
最佳答案
我不会在主视图中使用 drawRect,而是会为每个对象创建一个新的 subview 。这些 subview 可以是 UIImageView,或者是标准的 UIView,然后您可以通过继承 subview 的 drawRect 来继续绘制。
然后,使用 UIView 的动画方法为这些 subview 设置动画将变得微不足道:
[UIView animateWithDuration:0.3
animations:^(void) {
// Do something to the view...
}];
很明显,您随后需要添加代码来管理触摸并在需要时删除 subview ,但这应该不会太繁琐。
关于objective-c - 为响应触摸而设置动画(简单形状)的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399955/