Main.storybord的大致布局如下:
代码部分:
ViewController.m:
#import "ViewController.h" //枚举 typedef enum{kUpButton=1,kDownButton,kLeftButton,kRightButton,kMinifyButton,kPlusButton,kLeftRotateButton,kRightRotateButton}kTag; @interface ViewController () @property (weak, nonatomic) IBOutlet UIButton *imagButton; @property (weak, nonatomic) IBOutlet UIButton *upButton; @property (weak, nonatomic) IBOutlet UIButton *downButton; @property (weak, nonatomic) IBOutlet UIButton *leftButton; @property (weak, nonatomic) IBOutlet UIButton *rightButton; @property (weak, nonatomic) IBOutlet UIButton *minifyButton; @property (weak, nonatomic) IBOutlet UIButton *plusButton; @property (weak, nonatomic) IBOutlet UIButton *LeftRotateButton; @property (weak, nonatomic) IBOutlet UIButton *RightRotateButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //移动 - (IBAction)clickMoveButtons:(UIButton *)sender { CGAffineTransform transform = self.imagButton.transform; switch (sender.tag) { case kUpButton: transform =CGAffineTransformTranslate( transform, 0, -10); break; case kDownButton: transform =CGAffineTransformTranslate( transform, 0, 10); break; case kLeftButton: transform =CGAffineTransformTranslate( transform, -10, 0); break; case kRightButton: transform =CGAffineTransformTranslate( transform, 10, 0); break; } self.imagButton.transform=transform; [self changeButtonsState]; } //5 缩小 //6 放大 - (IBAction)clickScaleButtons:(UIButton *)sender { CGAffineTransform transform = self.imagButton.transform; switch (sender.tag) { case kMinifyButton: //实现缩小效果 transform = CGAffineTransformScale(transform, 0.95, 0.95); break; case kPlusButton: //实现放大效果 transform = CGAffineTransformScale(transform,1.05, 1.05); break; } self.imagButton.transform = transform; [self changeButtonsState]; } //禁用和释放按钮 -(void)changeButtonsState { //禁用和释放移动按钮 CGFloat topRangeOfButton = self.imagButton.frame.origin.y; CGFloat leftRangeOfButton = self.imagButton.frame.origin.x; CGFloat lowRangeOfButton = self.view.frame.size.height - topRangeOfButton - self.imagButton.frame.size.height; CGFloat rightRangeOfButton = self.view.frame.size.width - leftRangeOfButton - self.imagButton.frame.size.width; self.upButton.enabled=((topRangeOfButton-10)>=10); self.leftButton.enabled=((leftRangeOfButton-10)>=10); self.downButton.enabled=((lowRangeOfButton-10)>=10); self.rightButton.enabled=((rightRangeOfButton-10)>=10); //禁用和释放缩放按钮 self.plusButton.enabled =self.upButton.enabled && self.leftButton.enabled && self.downButton.enabled && self.rightButton.enabled; self.minifyButton.enabled = (self.imagButton.frame.size.width>=50); } //旋转 - (IBAction)clickRotateButton:(UIButton *)sender { CGAffineTransform transform = self.imagButton.transform; switch (sender.tag) { case kLeftRotateButton://逆时针 transform = CGAffineTransformRotate(transform, -M_PI_4); break; case kRightRotateButton: transform = CGAffineTransformRotate(transform, M_PI_2); break; } self.imagButton.transform = transform; } @end
上面是Transform实现图片的平移和缩放,实际上还有其他方法:
实现放大效果
Button.frame 以左上角为原点进行缩放
Button.bounds 以中心为原点进行缩放
实现移动效果
因为Button.bounds.origin是移动趋势,类似可读,是系统用来移动的,用户修改他得值是无法实现移动的,所以用中心点坐标center
Button.frame 以左上角为原点进行移动
Button.center 以中心为原点进行移动
frame和bounds的区别:
1、进行缩放不同
Button.frame 以左上角为原点进行缩放
Button.bounds 以中心为原点进行缩放
2、进行移动不同
Button.frame 以左上角为原点进行移动
Button.bounds 不能用来移动
3、出现的时机不同,Button.bounds 出现的早一些,很多时候,无发获取frame中得origin,但是可以通过bounds 来获取origin
比如用Frame来实现相同的功能,代码如下:
#import "ViewController.h" //枚举 typedef enum{kUpButton=1,kDownButton,kLeftButton,kRightButton,kMinifyButton,kPlusButton}kTag; @interface ViewController () @property (weak, nonatomic) IBOutlet UIButton *imagButton; @property (weak, nonatomic) IBOutlet UIButton *upButton; @property (weak, nonatomic) IBOutlet UIButton *downButton; @property (weak, nonatomic) IBOutlet UIButton *leftButton; @property (weak, nonatomic) IBOutlet UIButton *rightButton; @property (weak, nonatomic) IBOutlet UIButton *minifyButton; @property (weak, nonatomic) IBOutlet UIButton *plusButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //移动 - (IBAction)clickMoveButtons:(UIButton *)sender { CGRect rec = self.imagButton.frame; switch (sender.tag) { case kUpButton: rec.origin.y -=10; break; case kDownButton: rec.origin.y +=10; break; case kLeftButton: rec.origin.x -=10; break; case kRightButton: rec.origin.x +=10; break; } self.imagButton.frame=rec; [self changeButtonsState]; } //5 缩小 //6 放大 - (IBAction)clickScaleButtons:(UIButton *)sender { CGRect rec = self.imagButton.frame; switch (sender.tag) { case kMinifyButton: //实现缩小效果 rec.size.height -=10; rec.size.width -=10; //实现以图片的中心为原点进行缩小 rec.origin.x +=5; rec.origin.y +=5; break; case kPlusButton: //实现放大效果 rec.size.height +=10; rec.size.width +=10; //实现以图片的中心为原点进行放大 rec.origin.x -=5; rec.origin.y -=5; break; } self.imagButton.frame = rec; [self changeButtonsState]; } //禁用和释放按钮 -(void)changeButtonsState { //禁用和释放移动按钮 CGFloat topRangeOfButton = self.imagButton.frame.origin.y; CGFloat leftRangeOfButton = self.imagButton.frame.origin.x; CGFloat lowRangeOfButton = self.view.frame.size.height - topRangeOfButton - self.imagButton.frame.size.height; CGFloat rightRangeOfButton = self.view.frame.size.width - leftRangeOfButton - self.imagButton.frame.size.width; self.upButton.enabled=((topRangeOfButton-10)>=10); self.leftButton.enabled=((leftRangeOfButton-10)>=10); self.downButton.enabled=((lowRangeOfButton-10)>=10); self.rightButton.enabled=((rightRangeOfButton-10)>=10); //禁用和释放缩放按钮 self.plusButton.enabled =self.upButton.enabled && self.leftButton.enabled && self.downButton.enabled && self.rightButton.enabled; self.minifyButton.enabled = (self.imagButton.frame.size.width>=50); } @end
又或者用center实现移动,代码如下:
//移动 - (IBAction)clickMoveButtons:(UIButton *)sender { CGPoint point = self.imagButton.center; switch (sender.tag) { case kUpButton: point.y -= 10; break; case kDownButton: point.y +=10; break; case kLeftButton: point.x -=10; break; case kRightButton: point.x +=10; break; } self.imagButton.center=point; [self changeButtonsState]; }
用bouns实现缩放
- (IBAction)clickScaleButtons:(UIButton *)sender { CGRect rec = self.imagButton.bounds; switch (sender.tag) { case kMinifyButton: //实现缩小效果 rec.size.height -=10; rec.size.width -=10; break; case kPlusButton: //实现放大效果 rec.size.height +=10; rec.size.width +=10; break; } self.imagButton.bounds = rec; [self changeButtonsState]; }