gpt4 book ai didi

iphone - 如何做自旋转CALayer

转载 作者:行者123 更新时间:2023-12-03 21:21:48 25 4
gpt4 key购买 nike

当我单击 View 时,它会创建一个星形路径。

现在我想旋转添加到 CALayer 的星形。

谁能帮帮我......如何在 iPhone 上旋转 CALayer。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
currentPoint=[touch locationInView:self.view];
CGMutablePathRef starPath;
rootLayer = [CALayer layer];
rootLayer.frame = self.view.bounds;
[self.view.layer addSublayer:rootLayer];
starPath = CGPathCreateMutable();
CGPathMoveToPoint(starPath, NULL,currentPoint.x,currentPoint.y+15.0f);
for(int i = 1; i < 5; ++i)
{
CGFloat x =15.0*sinf(i * 4.0 * M_PI/5.0);
CGFloat y =15.0*cosf(i * 4.0 * M_PI/5.0);
CGPathAddLineToPoint(starPath, NULL,currentPoint.x+x,currentPoint.y+y);
}
CGPathCloseSubpath(starPath);
shapeLayer=[CAShapeLayer layer];
shapeLayer.path = starPath;
fillColor = [UIColor colorWithHue:0.825 saturation:1 brightness:1 alpha:1.0];
shapeLayer.fillColor = fillColor.CGColor;
[rootLayer addSublayer:shapeLayer];
CGPathRelease(starPath);
float zDistance=30;
CATransform3D aTransform=CATransform3DIdentity;
aTransform.m34=1.0/-zDistance;
rootLayer.sublayerTransform=aTransform;
[self addAnimationToLayer:rootLayer];
}

-(void)addAnimationToLayer:(CALayer *)layer
{
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
theAnimation.fromValue=[NSNumber numberWithDouble:M_PI_2];
//theAnimation.toValue=[NSNumber numberWithFloat:6.0f*M_PI/180.0f];
theAnimation.duration=100;
// theAnimation.autoreverses=TRUE;
theAnimation.speed=100;
theAnimation.repeatCount=10;
[layer addAnimation:theAnimation forKey:@"transform"] ;
}

最佳答案

像这样使用CAKeyframeAnimation:

CAKeyframeAnimation *spinAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
spinAnim.duration = 3.0; // some appropriate duration

float firstHalfRotation = M_PI;
float secondHalfRotation = (M_PI * 2.0);

spinAnim.values = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0, 0, 1)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(firstHalfRotation, 0, 0, 1)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(secondHalfRotation, 0, 0, 1)],
nil];
[layer addAnimation:spinAnim forKey:@"spin"];

关于iphone - 如何做自旋转CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com