欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 下拉刷新動(dòng)畫(huà)的實(shí)現(xiàn)實(shí)例

 更新時(shí)間:2017年05月26日 17:12:27   作者:Little_Mango  
這篇文章主要介紹了iOS 下拉刷新動(dòng)畫(huà)的實(shí)現(xiàn)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

先上完整的效果圖:


接下去動(dòng)畫(huà)分步實(shí)現(xiàn),首先先實(shí)現(xiàn)如下效果:

思路是這樣的,在偏移值小于等于100的時(shí)候繪制一個(gè)矩形,當(dāng)偏移值大于100的時(shí)候,底部直線變成曲線,主要是利用CAShapeLayer和UIBezierPath來(lái)實(shí)現(xiàn),代碼如下

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
  CGFloat height = -scrollView.contentOffset.y;
  UIBezierPath *path = [UIBezierPath bezierPath];
  [path moveToPoint:CGPointMake(0, 0)];
  [path addLineToPoint:CGPointMake(self.view.width, 0)];
  if (height <= 100) {
    //偏移值小于等于100的時(shí)候,底部繪制直線
    [path addLineToPoint:CGPointMake(self.view.width, height)];
    [path addLineToPoint:CGPointMake(0, height)];
  }else{
    //偏移值大于100的時(shí)候,底部繪制曲線
    [path addLineToPoint:CGPointMake(self.view.width, 100)];
    [path addQuadCurveToPoint:CGPointMake(0, 100) controlPoint:CGPointMake(self.view.center.x, height)];
  }
  [path closePath];
  self.shapeLayer.path = path.CGPath;
}

其次,實(shí)現(xiàn)繪制太陽(yáng),效果如下:

主要用到的是CAShapeLayer的path和strokeEnd屬性,首先繪制一個(gè)太陽(yáng),代碼如下:

#pragma mark - private method
-(void)p_initCircle {
  self.circleLayer.frame = CGRectMake(0, 0, self.view.width, 100);
  self.circleLayer.fillColor = nil;
  self.circleLayer.strokeColor = [UIColor whiteColor].CGColor;
  self.circleLayer.lineWidth = 2.0;

  CGPoint center = CGPointMake(self.view.center.x, 50);
  UIBezierPath *path = [UIBezierPath bezierPath];
  [path moveToPoint:CGPointMake(self.view.center.x, 35)];
  [path addArcWithCenter:center radius:15 startAngle:-M_PI_2 endAngle:M_PI * 1.5 clockwise:YES];
  CGFloat r1 = 17.0;
  CGFloat r2 = 22.0;
  for (int i = 0; i < 8 ; i++) {
    CGPoint pointStart = CGPointMake(center.x + sin((M_PI * 2.0 / 8 * i)) * r1, center.y - cos((M_PI * 2.0 / 8 * i)) * r1);
    CGPoint pointEnd = CGPointMake(center.x + sin((M_PI * 2.0 / 8 * i)) * r2, center.y - cos((M_PI * 2.0 / 8 * i)) * r2);
    [path moveToPoint:pointStart];
    [path addLineToPoint:pointEnd];
  }
  self.circleLayer.path = path.CGPath;
}

其次在拖拽的過(guò)程中,利用strokeEnd屬性逐步顯示太陽(yáng),代碼如下:

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
  CGFloat height = -scrollView.contentOffset.y;
  if (height <= 100) {
    //偏移值小于等于100的時(shí)候,繪制對(duì)應(yīng)的路徑
    self.circleLayer.strokeEnd = height / 100.0;
  }else{
    //偏移值大于100的時(shí)候,繪制全路徑
    self.circleLayer.strokeEnd = 1.0;
  }
}

其次,讓繪制完的太陽(yáng)隨著拽動(dòng)而旋轉(zhuǎn),效果圖如下:

主要思想是在繪制完畢之后,通過(guò)affineTransform屬性,讓太陽(yáng)圖層旋轉(zhuǎn),代碼如下:

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
  CGFloat height = -scrollView.contentOffset.y;
  [CATransaction begin];
  [CATransaction setDisableActions:YES];
  if (height <= 100) {
    //偏移值小于等于100的時(shí)候,不旋轉(zhuǎn)
    self.circleLayer.affineTransform = CGAffineTransformIdentity;
  }else{
    //偏移值小于等于100的時(shí)候,旋轉(zhuǎn)
    self.circleLayer.affineTransform = CGAffineTransformMakeRotation(-(M_PI / 720 * height - 100));
  }
  [CATransaction commit];
}

其中要注意的地方是需要禁止隱式動(dòng)畫(huà),不然每次修改affineTransform都會(huì)自動(dòng)帶上隱式動(dòng)畫(huà)效果。

最后就是釋放后,讓tableView滾動(dòng)到固定位置,然后讓太陽(yáng)持續(xù)旋轉(zhuǎn),屏蔽tableView的拖拽事件,主要用到的是CABaseAnimation,代碼如下

-(void)p_rise {
  self.tableView.scrollEnabled = NO;
  CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  anim.duration = 0.15;
  anim.toValue = @(M_PI / 4.0);
  anim.repeatCount = MAXFLOAT;
  [self.circleLayer addAnimation:anim forKey:nil];

}

最后在經(jīng)過(guò)一定時(shí)間后,移除圖層動(dòng)畫(huà),取消屏蔽tableView的拖拽事件,并且讓tableView滾動(dòng)到頂部,代碼如下:

-(void)p_stop {
  self.tableView.scrollEnabled = YES;
  [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
  [self.circleLayer removeAllAnimations];
}

源代碼傳送門(mén) 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論