【发布时间】:2013-06-19 10:18:26
【问题描述】:
目标
我正在尝试复制 UI Dynamics 拉动动画/行为。我在UIView 上设置了一个UIPanGestureRecognizer 用于拖动它。发生拖动时,我将触摸点设置为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) 应用于图层而不是视图CGAffineTransformRotate。但是闪烁仍然发生。有什么想法为什么会出现这个问题?我欢迎任何其他方法的建议,以达到相同的结果。
【问题讨论】:
-
我能够解决这个问题。请参阅下面的答案。
-
为什么要投反对票?有人不高兴?
标签: iphone ios calayer cgaffinetransform uipangesturerecognizer