目标
我正在尝试复制 UI Dynamics 拉动动画/行为。我已经UIPanGestureRecognizer
在我的身上设置了一个UIView
用于拖动它。当发生拖动时,我将触摸点设置为UIView.layer.anchorPoint
允许围绕触摸点旋转。在拖动过程中,我检查触摸速度方向并CGAffineTransformRotate
顺时针或逆时针应用。这就是我所说的。
问题
动画不流畅,UIView 在拖动和应用旋转时闪烁(在短实例中出现多个视图痕迹)。视频录制为 30fps,因此看不到闪烁。我希望我的解释清楚。
我的视图控制器中的源代码如下。我正在 iOS5.0+ 上尝试这个
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *animationView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
animationView.layer.backgroundColor = [UIColor redColor].CGColor;
animationView.layer.shouldRasterize = YES; // Does not help :(
animationView.autoresizingMask = UIViewAutoresizingNone; // Does not help :(
[self.view addSubview:animationView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[animationView addGestureRecognizer:pan];
[animationView release];
[pan release];
}
- (void) pan:(UIPanGestureRecognizer*) gesture {
if(gesture.state==UIGestureRecognizerStateBegan) {
CGPoint anchor = [gesture locationInView:gesture.view];
gesture.view.layer.anchorPoint = CGPointMake(anchor.x/gesture.view.bounds.size.width,
anchor.y/gesture.view.bounds.size.height);
gesture.view.center = CGPointMake(anchor.x/gesture.view.bounds.size.width,
anchor.y/gesture.view.bounds.size.height);
}
else if(gesture.state==UIGestureRecognizerStateChanged) {
CGPoint point1 = [gesture locationInView:self.view];
gesture.view.center=point1;
CGPoint velocity = [gesture velocityInView:gesture.view];
if(velocity.x > 0)
{
// Dragged right
if(velocity.y > 0) {
// Dragged down
// counter clock
CGFloat angle = -0.2;
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, angle);
}
else {
// Dragged up
// clock
CGFloat angle = 0.2;
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, angle);
}
}
else
{
// Dragged left
if(velocity.y > 0) {
// Dragged down
// clock
CGFloat angle = 0.2;
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, angle);
}
else {
// Dragged up
// counter clock
CGFloat angle = -0.2;
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, angle);
}
}
}
}
我曾尝试应用于CATransform3DMakeRotation (angle, 0.0f, 0.0f, 1.0f)
layer 而不是 view CGAffineTransformRotate
。但是闪烁仍然发生。有什么想法为什么会出现这个问题?我欢迎任何其他方法的建议,以达到相同的结果。