绕中心旋转的圆环

//本人只是提供一个思路,代码还需进一步完善(欢迎大牛小牛指点)


#import "CircleView.h"

@interface CircleView()

@property(nonatomic,strong)CABasicAnimation *rotationAnimation;

@end


#define  degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式

#define  PROGREESS_WIDTH  self.frame.size.width //圆直径

#define  PROGRESS_LINE_WIDTH 6 //弧线的宽度



@implementation CircleView

-(instancetype)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];

    self.backgroundColor=[UIColor clearColor];

    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];

    rotationAnimation.duration = 1;

    rotationAnimation.cumulative = YES;

    rotationAnimation.repeatCount = MAXFLOAT;

    [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    self.rotationAnimation=rotationAnimation;

    return self;

}

- (void)drawRect:(CGRect)rect {

    CAGradientLayer * gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = rect;

    gradientLayer.colors = @[(__bridge id)[[UIColor blackColor]colorWithAlphaComponent:0.1].CGColor,(__bridge id)[[UIColor whiteColor]colorWithAlphaComponent:0.3].CGColor,(__bridge id)[[UIColor whiteColor]colorWithAlphaComponent:0.5].CGColor,(__bridge id)[[UIColor whiteColor]colorWithAlphaComponent:0.6].CGColor,(__bridge id)[[UIColor whiteColor]colorWithAlphaComponent:0.8

                                                                                                                  ].CGColor,(__bridge id)[[UIColor whiteColor]colorWithAlphaComponent:1.0].CGColor];

    gradientLayer.startPoint = CGPointMake(0.1,0.0);

    gradientLayer.endPoint = CGPointMake(1,0.5);

    gradientLayer.locations=@[@0.15,@0.3,@0.45,@0.6,@0.75,@0.9];

    self.layer.mask=gradientLayer;

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(PROGREESS_WIDTH/2, PROGREESS_WIDTH/2) radius:(PROGREESS_WIDTH-PROGRESS_LINE_WIDTH)/2 startAngle:degreesToRadians(-160) endAngle:degreesToRadians(80) clockwise:YES];  //画弧线

    //center:弧线中心点的坐标,radius:弧线所在圆的半径, startAngle:弧线开始的角度值,endAngle:弧线结束的角度值 closewise:是否顺时针画弧线

    path.lineCapStyle=kCGLineCapRound;

    path.lineJoinStyle=kCGLineJoinRound;

    path.lineWidth=PROGRESS_LINE_WIDTH;

    UIColor *color=[UIColor redColor];

    [color setStroke];

    [path stroke];//连线

}

@end


 //使用

 CircleView *view=[[CircleView alloc]init];

  view.frame=CGRectMake(100, 100, 50, 50);

  [self.view addSubview:view];


坐标系圆环旋转通常涉及到三维空间中的旋转变换,特别是当我们要描述物体在固定轴上围另一个旋转中心(比如圆环中心旋转的情况。这个过程可以使用矩阵乘法来表示,其中旋转矩阵结合平移矩阵来完成。假设我们有一个直角坐标系XYZ,原点O是圆环中心圆环的半径分别为R和r(外径和内径),需要Z轴旋转θ角度。 首先,我们需要分两步进行变换: 1. **Z轴旋转**:这是通过旋转矩阵来完成的,对于Z轴逆时针旋转θ的角度,旋转矩阵(Z轴正方向看)为: \[ R_z(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \] 2. **圆环中心旋转**:如果物体还要相对于圆环中心旋转,先将坐标平移至圆环中心,然后进行旋转,最后再平移回原位置。平移矩阵T是一个加法操作,而复合平移就是先减去圆环中心的位置,再加上新的位置。 \[ T_{to\_center}(x', y', z') = \begin{bmatrix} 1 & 0 & 0 & -R \\ 0 & 1 & 0 & -r \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x' \\ y' \\ z' \\ 1 \end{bmatrix} \] \[ T_{back\_to\_origin}(x'', y'', z'') = \begin{bmatrix} 1 & 0 & 0 & R \\ 0 & 1 & 0 & r \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x'' \\ y'' \\ z'' \\ 1 \end{bmatrix} \] 最终变换为 \( P_{new} = T_{back\_to\_origin} \cdot R_z(\theta) \cdot T_{to\_center}(P_{old}) \),这里的\( P_{old} \)和\( P_{new} \)分别是原始和新坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值