在圆形方向上移动图像视图

时间:2011-12-13 04:43:52

标签: iphone cocoa-touch uiimageview core-animation core-graphics

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
CGRect frame = [button frame];
button.layer.anchorPoint = CGPointMake(0.5, 0.5);
button.layer.position = CGPointMake(frame.origin.x + 0.5 * frame.size.width, frame.origin.y + 0.5 * frame.size.height);
[CATransaction commit];

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];

CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
animation.delegate = self;
[button.layer addAnimation:animation forKey:@"rotationAnimation"];

[CATransaction commit];

我试过这个代码bt没用,我想在圆形路径中移动图像视图

2 个答案:

答案 0 :(得分:2)

请检查此代码以移动图片...谢谢

    UIBezierPath *customPath =[self createArcPath];

    UIImage *movingImage = [UIImage imageNamed:@"Unchecked.png"];
    CALayer *movingLayer = [CALayer layer];
    movingLayer.contents = (id)movingImage.CGImage;
    movingLayer.anchorPoint = CGPointZero;
    movingLayer.frame = CGRectMake(0.0f, 0.0f, movingImage.size.width, movingImage.size.height); // image position starting 
    [self.view.layer addSublayer:movingLayer];

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.duration = 4.0f;
    pathAnimation.path = customPath.CGPath;
    pathAnimation.calculationMode = kCAAnimationLinear;
    [movingLayer addAnimation:pathAnimation forKey:@"movingAnimation"];

}
  - (UIBezierPath *)createArcPath
    {
        UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
                                                             radius:10
                                                         startAngle:0
                                                           endAngle:2* M_PI
                                                          clockwise:YES];
        return aPath;
    }

答案 1 :(得分:1)

您可以在不使用CAAnimation类的情况下完成所需的动画。

在NSTimer对象中使用以下代码..

制作一个循环,其中角度变量从0开始到无穷大或360的倍数。

// radius是您想要移动ImageView的圆半径。

myImageView.center = CGPointMake(myImageView.center.x + cos(angle) * radius, myImageView.center.y + sin (angle) * radius);