IOS移动和方法小Demo

移动和放大一个图片基本操作:第一版
#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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值