移动和放大一个图片基本操作:第一版
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton * head;
- (IBAction)up;
- (IBAction)down;
- (IBAction)left;
- (IBAction)right;
- (IBAction)big;
- (IBAction)small;
@end
@implementation ViewController
#pragma mark 向上走
- (IBAction)up
{
//OC有一个语法,它不允许直接修改对象结构体属性的成员
//self.head.frame.origin.y -= 10;错误的写法
//它允许直接修改对象的结构体的属性
//简单说:直接改成员属性是改不了的,但是改了结构体再赋值就可以改
CGRect tempFrame = self.head.frame;
tempFrame.origin.y -= 10;
self.head.frame = tempFrame;
//总结:以后修改的时候,都是三部曲:1、取以前值给现在值,2、修改现在值,3、重新赋值
}
#pragma mark 向下走
- (IBAction)down
{
CGRect tempFrame = self.head.frame;
tempFrame.origin.y += 10;
self.head.frame = tempFrame;
}
#pragma mark 向左走
- (IBAction)left
{
CGRect tempFrame = self.head.frame;
tempFrame.origin.x -= 10;
self.head.frame = tempFrame;
}
#pragma mark 向右走
- (IBAction)right
{
CGRect tempFrame = self.head.frame;
tempFrame.origin.x += 10;
self.head.frame = tempFrame;
}
#pragma mark 放大
- (IBAction)big
{
CGRect tempFrame = self.head.frame;
tempFrame.size.width += 10;
tempFrame.size.height += 10;
tempFrame.origin.x -=5;
tempFrame.origin.y -=5;
self.head.frame = tempFrame;
}
#pragma mark 缩小
- (IBAction)small
{
CGRect tempFrame = self.head.frame;
tempFrame.size.width -= 10;
tempFrame.size.height -= 10;
tempFrame.origin.x +=5;
tempFrame.origin.y +=5;
self.head.frame = tempFrame;
}
@end
第二版
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton * head;
//方法有入参,这个入参就默认指向我们的控件
-(IBAction)move:(UIButton *)btn;
-(IBAction)zoom:(UIButton *)btn;
@end
@implementation ViewController
#pragma mark 移动
-(IBAction)move:(UIButton *)btn
{
CGRect frameRect = self.head.frame;
NSInteger currentTag = btn.tag;
CGFloat distance = 10;
switch (currentTag) {
case 10:
frameRect.origin.y -= distance;
break;
case 20:
frameRect.origin.y += distance;
break;
case 30:
frameRect.origin.x -= distance;
break;
case 40:
frameRect.origin.x += distance;
break;
default:
break;
}
self.head.frame = frameRect;
}
#pragma mark 扩大缩小
-(IBAction)zoom:(UIButton *)btn
{
CGRect frameRect = self.head.frame;
NSInteger viewTag = btn.tag;
NSInteger zommDistance = 10;
switch (viewTag) {
case 100:
frameRect.size.width += zommDistance;
frameRect.size.height += zommDistance;
frameRect.origin.x -= (zommDistance / 2);
frameRect.origin.y -= (zommDistance / 2);
break;
case 200:
frameRect.size.width -= zommDistance;
frameRect.size.height -= zommDistance;
frameRect.origin.x += (zommDistance /2);
frameRect.origin.y += (zommDistance /2);
break;
default:
break;
}
self.head.frame = frameRect;
}
@end
第三个版本,自己甄别,可能好的还是第二个版本,一个frame能解决问题
/**
版本三:常用的还是第二个版本
通过bounds改变图片的大小,
通过center就是对应的中点的位置来改变当前视图的位置
*/
CGPoint framePoint = self.head.center;
NSInteger moveDistance = 10;
switch (btn.tag) {
case 10:
framePoint.y -= moveDistance;
break;
case 20:
framePoint.y += moveDistance;
break;
case 30:
framePoint.x -= moveDistance;
break;
case 40:
framePoint.x += moveDistance;
break;
default:
break;
}
self.head.center = framePoint;
//版本三:常用的还是第二个版本
CGRect frameRect = self.head.bounds;
NSInteger zommDistance = 10;
switch (btn.tag) {
case 100:
frameRect.size.width += zommDistance;
frameRect.size.height += zommDistance;
frameRect.origin.x -= (zommDistance / 2);
frameRect.origin.y -= (zommDistance / 2);
break;
case 200:
frameRect.size.width -= zommDistance;
frameRect.size.height -= zommDistance;
frameRect.origin.x += (zommDistance /2);
frameRect.origin.y += (zommDistance /2);
break;
default:
break;
}
self.head.frame = frameRect;