圖形的初級變化使用View

來源:互聯網
上載者:User

標籤:

圖形的初級變化使用View

view就是一個試圖,而viewController就是這些試圖的一個容器--試圖控制器。以下內容是view的一小部分關聯,主要是按鈕的使用,試圖的變化。在Main.storyboard裡面添加控制項,如

試圖上的button分別是:

  1. switch按鈕。我們常見的一種按鈕,有開啟喝關閉兩種狀態。
  2. 灰色地區--一個view添加背景色為灰色,目的是讓讀者看見將要變化的一段地區。
  3. Action按鈕。使view變化顏色。
  4. Stop按鈕。使變化顏色和轉動暫停。
  5. rotation按鈕。點擊後使 view轉動。

代碼部分:

  1.在代碼中添加view、switch,_view.link.

 1 @property (weak, nonatomic) IBOutlet UIView *myView

2 @property (weak, nonatomic) IBOutlet UISwitch *mySwitch; 

{    UIView *_view;    //顏色數組    NSMutableArray *_colorArray;    }@property (nonatomic, strong) CADisplayLink *link;

 

  2.迴圈建立7個新的試圖view,並且為每一個view都添加背景色。隨機顏色的設定和固定顏色寫入。

 1     //建立視圖 並添加到父視圖 2     for (int i = 0 ; i < 7; i++) { 3         _view = [[UIView alloc] initWithFrame:CGRectMake(10 * i, 10 * i, self.myView.bounds.size.width - 2 * 10 * i, self.myView.bounds.size.height - 2 * 10 * i)]; 4         /* 5         //隨機顏色設定 6         _colorArray = [NSMutableArray arrayWithObjects: 7                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1], 8                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1], 9                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1],10                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1],11                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1],12                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1],13                        [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1], nil ];14         */15         16         //給顏色建立數組,(最麻煩的方法)17         _colorArray = [NSMutableArray arrayWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor grayColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor orangeColor], nil];18         //並且是每個view都有顏色19         _view.backgroundColor = _colorArray[i];20         //添加到view顯示21         [self.myView addSubview:_view];22         23     }

  3.顏色變化效果代碼及

 1 //開始動畫 2 - (IBAction)startAction:(UIButton *)sender { 3     //判斷switch是否開啟 4     if (_mySwitch.isOn) { 5   //每秒調用一次方法 6 //  [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(action:) userInfo:nil repeats:YES]; 7     //作用:只能點擊一次有效,點擊多次無效 8     if (self.link) { 9         return;10     }11     //每秒調用60次方法12     CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(action:)];13     14     [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];15     //設定全域的link16     self.link = link;17     }18 }19 20 - (void)action:(CADisplayLink *)timer{21 //- (void)action:(NSTimer *)timer{//使用NSTimer的時候22     //將每張視圖加入到數組裡面23     NSArray* subViews = [self.myView subviews];24     //迴圈遍曆數組,改變顏色25     for (int i = 0; i < 7; i++) {26         _view = subViews[i];27         //使顏色輪流變化28         if (i == 6) {29             //當顏色是最後一個時候,變為第一個30             _colorArray[i] = _colorArray[0];31         }else{32             //把顏色變為下一個33             _colorArray[i] = _colorArray[i+1];34         }35         //給每一個view添加背景色36         _view.backgroundColor = _colorArray[i];37         [self.myView addSubview:_view];38     }39 }
  1. NSTimer與CADisplayLink區別:NSTIimer每秒(可以設定秒數,比如0.1秒)調用一次,看著比較機械,而CADisplayLink每秒調用方法60次(重新整理60次),速度太快,看不清楚。
  2. 當switch開啟的時候,點擊Action有效果,而關閉後則點擊無效果。 

這是CADisplayLink,錄製效果不佳,實現效果是可以的

這是NSTimer(時間為1秒),使用NSTimer時候沒有暫停方法。

 

點擊Stop之後,會暫停,無論是顏色變化還是旋轉,都會暫停,使用的是同一個方法。

1 //停止按鈕2 - (IBAction)stop:(UIButton *)sender {3     //從所有runloop模式刪除對象(如果已經隱式地釋放接收機留存)和釋放的目標對象4     [self.link invalidate];5     //將self.link設定為nil6     self.link = nil;7 }

旋轉代碼:

 1 //旋轉按鈕 2 - (IBAction)rotation { 3     if (self.link)    return; 4      CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotationAct)]; 5     [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 6     self.link = link; 7 } 8 //旋轉動畫 9 - (void)rotationAct {10     //這是轉動效果11     //M_PI/100是因為轉動太快,如此之後看著正常12     self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI/100);13         //開始緩慢14         [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];15         [self.myView addSubview:_view];16

總結:

  1. CADisplayLinkd的使用以及停止的使用。
  2. 角度旋轉。transform的使用
  3. 顏色互相變化的方法。

 

歡迎讀者提出寶貴意見,歡迎查閱!如需轉載,請標明出處。

 

圖形的初級變化使用View

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.