用Animation實現iphone動畫效果

來源:互聯網
上載者:User

  1、寫了一個小Demo,實現上下移動的:

 

  主要代碼如下:

  - (IBAction)upChange:(id)sender {

  if (typeView == down) {

  [self.downBtn setHidden:NO];

  [self.upBtn setHidden:NO];

  [UIView animateWithDuration:0.5 animations:^{

  self.upView.center = CGPointMake(self.upView.center.x, 0);

  self.downView.center = CGPointMake(self.downView.center.x, (self.upView.frame.size.height+self.downView.frame.size.height)/2);

  }];

  typeView = middle;

  }else if(typeView == middle){

  [self.downBtn setHidden:NO];

  [self.upBtn setHidden:YES];

  [UIView animateWithDuration:0.5 animations:^{

  self.upView.center = CGPointMake(self.upView.center.x, -(self.upView.frame.size.height/2));

  self.downView.center = CGPointMake(self.downView.center.x, self.downView.frame.size.height/2);

  }];

  typeView = up;

  }

  }

  - (IBAction)downChange:(id)sender {

  if (typeView == middle) {

  [self.downBtn setHidden:YES];

  [self.upBtn setHidden:NO];

  [UIView animateWithDuration:0.5 animations:^{

  self.upView.center = CGPointMake(self.upView.center.x, self.upView.frame.size.height/2);

  self.downView.center = CGPointMake(self.downView.center.x, self.upView.frame.size.height+self.downView.frame.size.height/2);

  }];

  typeView = down;

  }else if(typeView == up)

  {

  [self.downBtn setHidden:NO];

  [self.upBtn setHidden:NO];

  [UIView animateWithDuration:0.5 animations:^{

  self.upView.center = CGPointMake(self.upView.center.x, 0);

  self.downView.center = CGPointMake(self.downView.center.x, (self.upView.frame.size.height+self.downView.frame.size.height)/2);

  }];

  typeView = middle;

  }

  }

  這是主要的代碼,另外還有一些需要注意的一些小細,如你的兩個view的大小,需要計算出精確位置。

  下面開始學習[UIView animateWithDuration]方法的使用:

  函數原型:

  + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL

  duration:動畫期間

  animations:該代碼兒塊中是所有view要完成的動畫的所有屬性的定義;

  還有另外兩個函數:

  + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

  + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);// delay = 0.0, options = 0

  options:是動畫執行的選項

  completion:是動畫完成以後所要執行的代碼塊兒。

相關文章

聯繫我們

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