使用贝塞尔曲线将图像移动90度

时间:2015-02-18 06:50:05

标签: ios uiimageview uibezierpath cgaffinetransform

我是iOS新手,请帮助我使用Bezier曲线沿90度路径移动图像。我曾经搜查过,但我的疑问还不清楚,所以请帮助我。

我曾尝试过这段代码,但我想使用Bezier曲线。

- (void)startApp {
  UIImageView *imageToMove =
      [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"man.jpg"]];
  imageToMove.frame = CGRectMake(10, 10, 100, 100);
  [self.view addSubview:imageToMove];

  // Move the image
  [self moveImage:imageToMove
         duration:1.0
            curve:UIViewAnimationCurveLinear
                x:70.0
                y:70.0];
}

- (void)moveImage:(UIImageView *)image
         duration:(NSTimeInterval)duration
            curve:(int)curve
                x:(CGFloat)x
                y:(CGFloat)y {
  // Setup the animation
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:duration];
  [UIView setAnimationCurve:curve];
  [UIView setAnimationBeginsFromCurrentState:YES];

  // The transform matrix
  CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
  image.transform = transform;

  // Commit the changes
  [UIView commitAnimations];
}

2 个答案:

答案 0 :(得分:0)

您也可以使用CABasicAnimation旋转(转换)视图。我做了一些计算。

- (void) rotateAnimation {

    CABasicAnimation *topAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    topAnimation.autoreverses = NO;
    topAnimation.duration = 0.8;
    topAnimation.repeatDuration = 0;
    topAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DPerspect(CATransform3DMakeRotation(M_PI, 1, 0, 0), CGPointMake(0, 0), 400)];
    [image.layer addAnimation:topAnimation forKey:nil];
}

这种轮换的支持方法如下:

CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
{
    CATransform3D transToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
    CATransform3D transBack = CATransform3DMakeTranslation(center.x, center.y, 0);
    CATransform3D scale = CATransform3DIdentity;
    scale.m34 = -1.0f/disZ;
    return CATransform3DConcat(CATransform3DConcat(transToCenter, scale), transBack);
}

CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
{
    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
}

关于旋转在各层上的X轴(水平)上。您可以在rotateAnimation中从下面的行将轴值更改为Y和Z.

CATransform3DMakeRotation(M_PI, 1, 0, 0), CGPointMake(0, 0), 400)]

通过在上面的代码中将1, 0, 0值更改为0, 1, 0,将更改Y轴上的旋转,并且类似地更改Z轴。替换值并寻找您喜欢的旋转。

答案 1 :(得分:0)

这是我的UIImageView,其中IBOutlet名为drawPad。

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
radius:100
startAngle:0
endAngle:(22.0f/7.0f)
clockwise:YES];

UIGraphicsBeginImageContext(self.view.frame.size);

path.lineCapStyle = kCGLineCapRound;
path.lineWidth = 10.0f;

[[UIColor blackColor] setStroke];
[path stroke];

self.drawpad.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();