多语言展示
当前在线:1823今日阅读:27今日分享:41

iOS程序-UIView的常见属性

1.通过frame属性控制按钮移动2.通过tag标记获取特定的按钮3.通过transform控制按钮放大\缩小:CGAffineTransformScale(_btn.transform, scale, scale)4.通过transform控制按钮旋转角度:CGAffineTransformRotate(_btn.transform, M_PI_4 * 1);
方法/步骤
1

MJViewController.h#import @interface MJViewController : UIViewController@property (weak, nonatoic) IBOutlet UIButton *btn;// 行走- (IBAction)run:(id)sender;// 缩放- (IBAction)scale:(id)sender;// 旋转- (IBAction)rotate:(id)sender;@end

2

MJViewController.m#import 'MJViewController.h'#define kDelta 50//const int delta = 50;@interface MJViewController ()@end@implementation MJViewController#pragma mark 控制按钮走动(上下左右)- (IBAction)run:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.先取出frame    CGRect tempFrame = _btn.frame;        // 2.取出按钮的tag标记    int tag = [sender tag];    // CGFloat delta = 100;    switch (tag) {        case 1: // 上            tempFrame.origin.y -= kDelta;            break;                    case 2: // 右            tempFrame.origin.x += kDelta;            break;                    case  3: // 下            tempFrame.origin.y += kDelta;            break;                    case 4: // 左            tempFrame.origin.x -= kDelta;            break;                    default:            break;    }        // 3.重新赋值按钮的frame    _btn.frame = tempFrame;        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}#pragma mark 放大\缩小- (IBAction)scale:(id)sender {    // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 1.计算缩放比例    CGFloat scale = [sender tag] == 20 ? 1.2 : 0.8;    // 2.修改按钮形变属性    _btn.transform = CGAffineTransformScale(_btn.transform, scale, scale);        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}#pragma mark 左旋转\右旋转- (IBAction)rotate:(id)sender {//    _angle -= M_PI_4;        // 0.动画(头部-开始动画)    [UIView beginAnimations:nil context:nil];    // 设置动画的执行时间    [UIView setAnimationDuration:1.0];        // 弧度 3.14 - π    // 角度 180    // 向左旋转45°//    _btn.transform = CGAffineTransformMakeRotation(- M_PI_4);//    _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * (10 == tag?-1:1));        int tag = [sender tag];    if (10 == tag) { // 左        _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * -1);    } else { // 右        _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * 1);    }        // 4.动画(尾部-提交动画-执行动画)    [UIView commitAnimations];}@end

推荐信息