首页 > 代码库 > 【iOS开发-43】万能的transform注意事项,以及viewWithTag以及.png后缀可以省略的一些知识
【iOS开发-43】万能的transform注意事项,以及viewWithTag以及.png后缀可以省略的一些知识
注意事项:
(1)图片如果是png格式的话,在代码中可以省略后缀。
(2)可以给控件一个tag值,然后用viewWithTag获取这个控件。
(3)transform的上下左右移动是按照它的上下左右边框垂直的方向移动的,即如果你把控件旋转了,那么上下左右就不是传统的上下左右,而是斜着的上下左右。
(4)transform可以实现上下左右旋转缩放等效果,但它是相对于一个位置而言的,如果相对位置不变,则相当于只生效一次,而如果每次都把改变后的位置重新设置为新的相对位置,则每次都会生效。即代码中,有两种方法。
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { //创建一个btn,里面是图像 UIButton *btnImg=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnImg.frame=CGRectMake(130, 120, 100, 100); //如果是.png的图像,可以省略.png后缀 [btnImg setBackgroundImage:[UIImage imageNamed:@"hi"] forState:UIControlStateNormal]; //增加一个tag,用于后续获取这个btnImg btnImg.tag=1; [self.view addSubview:btnImg]; //创建上下左右、左右旋转、放大缩小按钮 UIButton *btnUp=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnUp.frame=CGRectMake(80, 400, 20, 20); [btnUp setTitle:@"上" forState:UIControlStateNormal]; [btnUp addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnUp]; UIButton *btnDown=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnDown.frame=CGRectMake(80, 460, 20, 20); [btnDown setTitle:@"下" forState:UIControlStateNormal]; [btnDown addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnDown]; UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnLeft.frame=CGRectMake(50, 430, 20, 20); [btnLeft setTitle:@"左" forState:UIControlStateNormal]; [btnLeft addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnLeft]; UIButton *btnRight=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnRight.frame=CGRectMake(110, 430, 20, 20); [btnRight setTitle:@"右" forState:UIControlStateNormal]; [btnRight addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnRight]; UIButton *btnRoLeft=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnRoLeft.frame=CGRectMake(150, 430, 40, 20); [btnRoLeft setTitle:@"左转" forState:UIControlStateNormal]; [btnRoLeft addTarget:self action:@selector(roLeft) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnRoLeft]; UIButton *btnRoRight=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnRoRight.frame=CGRectMake(200, 430, 40, 20); [btnRoRight setTitle:@"右转" forState:UIControlStateNormal]; [btnRoRight addTarget:self action:@selector(roRight) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnRoRight]; UIButton *btnBig=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnBig.frame=CGRectMake(250, 430, 40, 20); [btnBig setTitle:@"放大" forState:UIControlStateNormal]; [btnBig addTarget:self action:@selector(big) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnBig]; UIButton *btnSmall=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnSmall.frame=CGRectMake(300, 430, 40, 20); [btnSmall setTitle:@"缩小" forState:UIControlStateNormal]; [btnSmall addTarget:self action:@selector(small) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnSmall]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } //只有一个按钮的时候,就没必要传递这个按钮参数过来 -(void)up{ //用viewWithTag获取UIView对象,如果我们知道具体是哪种UIView,可以强制转换一下 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; //以下语句是向上移动10,但按钮只生效一次,因为它相当于赋值ty=-10,且它的移动只相对于最原始位置,所以相对于的那个原始位置不变,而且ty=-10不变,则不再移动 //要么每次点击按钮改变ty的值,要么用不断改变它相对于的位置,即把最新的位置设置为它的相对于的位置 //btnImg1.transform=CGAffineTransformMakeTranslation(0, -10); btnImg1.transform=CGAffineTransformTranslate(btnImg1.transform, 0, -10); } -(void)down{ //同理,如下 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; btnImg1.transform=CGAffineTransformTranslate(btnImg1.transform, 0, 10); } -(void)left{ //同理,如下 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; btnImg1.transform=CGAffineTransformTranslate(btnImg1.transform, -10, 0); } -(void)right{ //同理,如下 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; btnImg1.transform=CGAffineTransformTranslate(btnImg1.transform, 10, 0); } -(void)roLeft{ UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; //同样,第一句只能点击生效1次,且用的是弧度制的数字,pi/4 //btnImg1.transform=CGAffineTransformMakeRotation(-M_PI_4); btnImg1.transform=CGAffineTransformRotate(btnImg1.transform, -M_PI_4); } -(void)roRight{ //同理 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; btnImg1.transform=CGAffineTransformRotate(btnImg1.transform, M_PI_4); } -(void)big{ UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; //同样,第一句只能点击生效1次,且用的是弧度制的数字,pi/4 //btnImg1.transform=CGAffineTransformMakeScale(1.2, 1.2); btnImg1.transform=CGAffineTransformScale(btnImg1.transform, 1.2, 1.2); } -(void)small{ //同理 UIButton *btnImg1=(UIButton *)[self.view viewWithTag:1]; btnImg1.transform=CGAffineTransformScale(btnImg1.transform, 0.8, 0.8); } @end
结果:
【iOS开发-43】万能的transform注意事项,以及viewWithTag以及.png后缀可以省略的一些知识
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。