【iOS開發-43】萬能的transform注意事項,以及viewWithTag以及.png尾碼可以省略的一些知識

來源:互聯網
上載者:User

標籤:http   io   os   ar   for   2014   on   cti   代碼   

注意事項:


(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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.