【发布时间】:2011-12-06 12:35:21
【问题描述】:
我有一个自定义的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 */
}
【问题讨论】:
标签: objective-c ios uiview core-animation core-graphics