2011-10-24 55 views

回答

2

這是一條直線嗎?..您可以使用1個像素寬的UIView併爲其框架屬性設置動畫。使用[UIView beginAnimations][UIView commitAnimations];否則,see this post

編輯響應

使用核心動畫,你可以做這樣的事情(如按鈕後面)

CABasicAnimation *theAnimation = [self pathAnimation]; 
theAnimation.duration=3.0; 
theAnimation.autoreverses=YES; 
[[self layer] addAnimation:theAnimation forKey:@"animatePath"]; 

路徑動畫的定義爲:

- (CAAnimation*)pathAnimation; 
{ 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path,NULL,50.0,120.0); 

    CGPathAddCurveToPoint(path,NULL,50.0,275.0, 
          150.0,275.0, 
          150.0,120.0); 
    CGPathAddCurveToPoint(path,NULL,150.0,275.0, 
          250.0,275.0, 
          250.0,120.0);  
    CGPathAddCurveToPoint(path,NULL,250.0,275.0, 
          350.0,275.0, 
          350.0,120.0);  
    CGPathAddCurveToPoint(path,NULL,350.0,275.0, 
          450.0,275.0, 
          450.0,120.0); 

    CAKeyframeAnimation * 
     animation = [CAKeyframeAnimation 
        animationWithKeyPath:@"position"]; 

    [animation setPath:path]; 
    [animation setDuration:3.0]; 

    [animation setAutoreverses:YES]; 

    CFRelease(path); 

    return animation; 

} 

這是隻有一個指向你可以用Core Animation做什麼的指針。詳細信息請參見this article

+0

感謝您的答覆。我已經嘗試過,但它不工作。你能給我一個很好的示例代碼。它不僅是狀態線,它還包含弧。像CGContextAddArc(context,cellSize,0,32,-90,-180,1);請建議。 –

+0

我已經添加了迴應,給你一個指示你可以做什麼。您絕對需要使用Core Animation關鍵幀動畫來做到這一點。 – zakishaheen

12

下面是答案(這裏找到:http://soulwithmobiletechnology.blogspot.fr/2012/07/how-to-animate-line-draw.html

UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(50.0,0.0)]; 
[path addLineToPoint:CGPointMake(120.0, 600.0)]; 

CAShapeLayer *pathLayer = [CAShapeLayer layer]; 
pathLayer.frame = self.view.bounds; 
pathLayer.path = path.CGPath; 
pathLayer.strokeColor = [[UIColor redColor] CGColor]; 
pathLayer.fillColor = nil; 
pathLayer.lineWidth = 2.0f; 
pathLayer.lineJoin = kCALineJoinBevel; 

[self.view.layer addSublayer:pathLayer]; 

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
pathAnimation.duration = 2.0; 
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 

不要忘記#import <QuartzCore/QuartzCore.h>

+0

這是非常有用的,但我怎樣才能開始一個新的動畫時,它結束了?提前致謝。 –

+2

@yucelbayram設置動畫的委託,然後實現委託方法: - (void)animationDidStop :(CAAnimation *)動畫完成:(布爾)標誌 – Elechtron

+0

@AskeAnker謝謝,我會試試這個。 –

0

雨燕3.0版本馬丁的回答(檢查和Xcode 8.3.3)

let path = UIBezierPath() 
    path.move(to: CGPoint(x: 50, y: 0)) 
    path.addLine(to: CGPoint(x: 120, y: 600)) 

    let pathLayer = CAShapeLayer() 
    pathLayer.frame = view.bounds 
    pathLayer.path = path.cgPath 
    pathLayer.strokeColor = UIColor.red.cgColor 
    pathLayer.fillColor = nil 
    pathLayer.lineWidth = 2 
    pathLayer.lineJoin = kCALineJoinBevel 
    view.layer.addSublayer(pathLayer) 

    let pathAnimation = CABasicAnimation(keyPath: "strokeEnd") 
    pathAnimation.duration = 2.0 
    pathAnimation.fromValue = 0 
    pathAnimation.toValue = 1 
    pathLayer.add(pathAnimation, forKey: "strokeEnd")