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)
应用于图层而不是视图CGAffineTransformRotate
。但闪烁仍然发生。有什么想法可以解决这个问我欢迎任何其他方法的建议,以实现相同的结果。
答案 0 :(得分:-2)
经过一番认真思考解决这个问题,我意识到这是一个微不足道的错误。引起闪烁是因为我正在进行旋转以使拖动方向发生最轻微的变化。为了解决这个问题,我在进行旋转之前假设了像素变化的任意余量。
解决问题的修改后的代码如下所示
- (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);
}
else if(gesture.state==UIGestureRecognizerStateChanged) {
CGPoint point1 = [gesture locationInView:self.view];
gesture.view.layer.position=point1;
CGPoint velocity = [gesture velocityInView:gesture.view];
CGFloat angle = 0.0;
if(velocity.x > self.view.bounds.size.width*0.15)
{
// Dragged right
if(velocity.y > self.view.bounds.size.height*0.02) {
// Dragged down
// counter clock
angle = -0.05;
}
else if (velocity.y < -self.view.bounds.size.height*0.02) {
// Dragged up
// clock
angle = 0.05;
}
}
else if (velocity.x < -self.view.bounds.size.width*0.15)
{
// Dragged left
if(velocity.y > self.view.bounds.size.height*0.02) {
// Dragged down
// clock
angle = 0.05;
}
else if (velocity.y < -self.view.bounds.size.height*0.02) {
// Dragged up
// counter clock
angle = -0.05;
}
}
[self applyRotationToView:gesture.view byAngle:angle];
}
}
- (void) applyRotationToView:(UIView*)rotationView byAngle:(CGFloat)angle {
rotationView.transform = CGAffineTransformRotate(rotationView.transform, angle);
}
动画仍然没有人们想象的那么顺利,但是我已经设法减少恼人的闪烁。