// 實現圖片的旋轉和移動和放大縮小 #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