view沿着某一点旋转

#import "JKZhuanPanTopBtnsView.h"

CGFloat const topBtnsHeight = 400.0f;

@interface JKZhuanPanTopBtnsView ()

/**<#condition#>*/

@property (nonatomic,strong)UIView * tempView;

@property (nonatomic,assign) float angle;

@end

@implementation JKZhuanPanTopBtnsView

CGAffineTransform  GetCGAffineTransformRotateAroundPoint(float centerX, float centerY ,float x ,float y ,float angle)

{

    x = x - centerX; //计算(x,y)从(0,0)为原点的坐标系变换到(CenterX ,CenterY)为原点的坐标系下的坐标

    y = y - centerY; //(0,0)坐标系的右横轴、下竖轴是正轴,(CenterX,CenterY)坐标系的正轴也一样

    

    CGAffineTransform  trans = CGAffineTransformMakeTranslation(x, y);

    trans = CGAffineTransformRotate(trans,angle);

    trans = CGAffineTransformTranslate(trans,-x, -y);

    return trans;

}

- (instancetype)initWithFrame:(CGRect)frame {

    if (self == [super initWithFrame:frame]) {

        self.backgroundColor = [UIColor redColor];

        

        

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0 , 50, 50)];

        view.center = self.center;

        view.backgroundColor = [UIColor greenColor];

        [self addSubview:view];

        self.tempView = view;

        

        self.angle = 45.0/180.0*M_PI;

        

       

    }

    return self;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    float centerX = self.tempView.bounds.size.width/2;

    float centerY = self.tempView.bounds.size.height/2;

    

//    float centerX = self.bounds.size.width/2;

//    float centerY = self.bounds.size.height/2;

    

//    float x = self.tempView.bounds.size.width/2;

//    float y = self.tempView.bounds.size.height;

        float x = 50;

        float y = 50;

    

    

    CGAffineTransform trans = GetCGAffineTransformRotateAroundPoint(centerX,centerY ,x ,y ,self.angle);

    

    self.angle += 45.0/180.0*M_PI;

    

    self.tempView.transform = CGAffineTransformIdentity;

    self.tempView.transform = trans;

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

@end

原文地址:https://www.cnblogs.com/Jackie-subDai/p/9336094.html