(纯代码 )transform旋转:
// 实现图片的旋转和移动和放大缩小 #import "ViewController.h" @interface ViewController () @property (nonatomic, weak) UIButton *_btnImage; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /** 头像图片 */ // 设置按钮为自定义样式 UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom]; // 设置位置和尺寸 btnImage.frame = CGRectMake(50, 50, 284, 312); // 高光 // 设置文字及对应状态 [btnImage setTitle:@"高光啊。。。。" forState:UIControlStateHighlighted]; // 设置文字颜色及对应状态 [btnImage setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; // 设置文字样式和大小 btnImage.titleLabel.font = [UIFont fontWithName:nil size:35.0]; [btnImage setBackgroundImage:[UIImage imageNamed:@"biaoqingdi"] forState:UIControlStateHighlighted]; // 普通 [btnImage setTitle:@"普通啊。。。。" forState:UIControlStateNormal]; [btnImage setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal]; [btnImage setBackgroundImage:[UIImage imageNamed:@"chiniupa"] forState:UIControlStateNormal]; [self.view addSubview:btnImage]; // 方面以后使用或修改控件属性 self._btnImage = btnImage; /** 左旋转 */ UIButton *btnLeftRotate = [UIButton buttonWithType:UIButtonTypeCustom]; btnLeftRotate.frame = CGRectMake(285, 400, 50, 50); [btnLeftRotate setImage:[UIImage imageNamed:@"left_rotate_normal"] forState:UIControlStateNormal]; [btnLeftRotate setImage:[UIImage imageNamed:@"left_rotate_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnLeftRotate]; [btnLeftRotate addTarget:self action:@selector(btnRotate:) forControlEvents:UIControlEventTouchUpInside]; [btnLeftRotate setTag:1]; /** 右旋转 */ UIButton *btnRightRotate = [UIButton buttonWithType:UIButtonTypeCustom]; btnRightRotate.frame =CGRectMake(285, 500, 50, 50); [btnRightRotate setImage:[UIImage imageNamed:@"right_rotate_normal"] forState:UIControlStateNormal]; [btnRightRotate setImage:[UIImage imageNamed:@"right_rotate_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnRightRotate]; [btnRightRotate addTarget:self action:@selector(btnRotate:) forControlEvents:UIControlEventTouchUpInside]; [btnRightRotate setTag:2]; /** 向上移动 */ UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeCustom]; btnUp.frame = CGRectMake(100, 400, 50, 50); [btnUp setImage:[UIImage imageNamed:@"top_normal"] forState:UIControlStateNormal]; [btnUp setImage:[UIImage imageNamed:@"top_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnUp]; [btnUp setTag:1]; [btnUp addTarget:self action:@selector(btnRun:) forControlEvents:UIControlEventTouchUpInside]; /** 向左移动 */ UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom]; btnLeft.frame = CGRectMake(50, 450, 50, 50); [btnLeft setImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [btnLeft setImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnLeft]; [btnLeft setTag:2]; [btnLeft addTarget:self action:@selector(btnRun:) forControlEvents:UIControlEventTouchUpInside]; /** 向右移动 */ UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom]; btnRight.frame = CGRectMake(150, 450, 50, 50); [btnRight setImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [btnRight setImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnRight]; [btnRight setTag:3]; [btnRight addTarget:self action:@selector(btnRun:) forControlEvents:UIControlEventTouchUpInside]; /** 向下移动 */ UIButton *btnDown = [UIButton buttonWithType:UIButtonTypeCustom]; btnDown.frame = CGRectMake(100, 500, 50, 50); [btnDown setImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal]; [btnDown setImage:[UIImage imageNamed:@"bottom_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnDown]; [btnDown setTag:4]; [btnDown addTarget:self action:@selector(btnRun:) forControlEvents:UIControlEventTouchUpInside]; /** 放大 */ UIButton *btnBig = [UIButton buttonWithType:UIButtonTypeCustom]; btnBig.frame = CGRectMake(100, 600, 50, 50); [btnBig setImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal]; [btnBig setImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnBig]; [btnBig setTag:1]; [btnBig addTarget:self action:@selector(btnFiex:) forControlEvents:UIControlEventTouchUpInside]; /** 缩小 */ UIButton *btnSmall = [UIButton buttonWithType:UIButtonTypeCustom]; btnSmall.frame = CGRectMake(285, 600, 50, 50); [btnSmall setImage:[UIImage imageNamed:@"minus_normal"] forState:UIControlStateNormal]; [btnSmall setImage:[UIImage imageNamed:@"minus_highlighted"] forState:UIControlStateHighlighted]; [self.view addSubview:btnSmall]; [btnSmall setTag:2]; [btnSmall addTarget:self action:@selector(btnFiex:) forControlEvents:UIControlEventTouchUpInside]; } // 旋转 - (void) btnRotate: (UIButton *) btn { switch (btn.tag) { case 1: // __btnImage等同于self._btnImage // 负数角度为向左旋转角度 // 这个方法一样的效果:__btnImage.transform = CGAffineTransformMakeRotation(-M_PI_4); __btnImage.transform = CGAffineTransformRotate(__btnImage.transform, -M_PI_4); break; case 2: // 正数角度为向右旋转角度 self._btnImage.transform = CGAffineTransformRotate(self._btnImage.transform, M_PI_4); } } // 移动 - (void) btnRun: (UIButton *) btn { switch (btn.tag) { case 1: // 每次向上移动的距离 // 这个方法一样的效果:self._btnImage.transform = CGAffineTransformMakeTranslation(0, -30); self._btnImage.transform = CGAffineTransformTranslate(self._btnImage.transform, 0, -30); break; case 2: // 每次向左移动30的距离 self._btnImage.transform = CGAffineTransformTranslate(self._btnImage.transform, -30, 0); break; case 3: // 每次向右移动30的距离 self._btnImage.transform = CGAffineTransformTranslate(self._btnImage.transform, 30, 0); break; case 4: // 每次向下移动30的距离 self._btnImage.transform = CGAffineTransformTranslate(self._btnImage.transform, 0, 30); break; } } // 放大和缩小 - (void) btnFiex:(UIButton *) btn { switch (btn.tag) { case 1: // 每次放大1.5倍 // 这个方法一样的效果:self._btnImage.transform = CGAffineTransformMakeScale(1.5, 1.5); self._btnImage.transform = CGAffineTransformScale(self._btnImage.transform, 1.5, 1.5); break; case 2: // 每次缩小0.5倍 self._btnImage.transform = CGAffineTransformScale(self._btnImage.transform, 0.5, 0.5); break; } } @end