使用CADisplayLink的UIImageView旋转动画 - 跟踪旋转

时间:2015-12-02 23:09:51

标签: ios uiimageview rotation cadisplaylink

我被建议尝试CADisplayLink来轮换UIIMageView。我正在使用Storyboard构建这个应用程序。

我需要在点击屏幕时启动和停止动画。我还需要跟踪点击屏幕时旋转停止的角度。

我找到了这个教程HERE,但这对我没有帮助。

我还发现了一些小例子(here's one),但我无法让它实际动画。从我从上面的例子中得到的,我的图像旋转一次,然后停止。

除此之外,我真的找不到一个好的教程或示例,说明如何使用CADisplayLink连续旋转我的UIImageView,直到点击,然后再次点击再继续旋转,最后存储旋转角度。

为了让您了解我想要实现的目标,它基本上是一个钟面,当钟针旋转时,您可以通过点击来启动和停止它,并检索角度。

有谁知道一个很好的例子吗?或者也许可以帮助我。感谢。

更新/编辑

在我的应用程序中使用Rob的令人敬畏的Obj-C答案非常有效。 如果有人对此感兴趣,我就会跟踪旋转角度并将其显示在屏幕上:

每次调用HandleDisplayLink时,我都会调用此方法:[self calculateDisplayAngle];

方法很简单:

 - (void)calculateDisplayAngle{
         currentRotationAngle = self.percent*360; //360 because I changed kDuration = 1;
         self.AngleLabel.text = [NSString stringWithFormat:@"Angle: %.2f",currentRotationAngle];
 }

1 个答案:

答案 0 :(得分:2)

您可以拥有属性来跟踪旋转状态,这样当您停止旋转时,它可以从中断处继续。例如:

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    startDisplayLink()
}

@IBAction func handleTapGesture(sender: UITapGestureRecognizer) {
    if displayLink != nil {
        stopDisplayLink()
    } else {
        startDisplayLink()
    }
}

private let duration = 2.5
private var percent = 0.0
private var displayLink: CADisplayLink?
private var start: CFAbsoluteTime?

func startDisplayLink() {
    displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    start = CFAbsoluteTimeGetCurrent() + (1.0 - percent) * duration
    displayLink?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
}

func stopDisplayLink() {
    displayLink?.invalidate()
    displayLink = nil
}

func handleDisplayLink(displayLink: CADisplayLink) {
    let elapsed = CFAbsoluteTimeGetCurrent() - start!
    percent = (elapsed % duration) / duration
    imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2.0 * percent))
}

或在Objective-C中:

static CGFloat const kDuration = 2.5;

@interface ViewController2 ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CFAbsoluteTime startTime;
@property (nonatomic) CGFloat percent;

@end

@implementation ViewController2

- (IBAction)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (self.displayLink) {
        [self stopDisplayLink];
    } else {
        [self startDisplayLink];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self startDisplayLink];
    // Do any additional setup after loading the view.
}


- (void)startDisplayLink {
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    self.startTime = CFAbsoluteTimeGetCurrent() + (1.0 - self.percent) * kDuration;
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink {
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink {
    CFAbsoluteTime elapsed = CFAbsoluteTimeGetCurrent() - self.startTime;
    self.percent = elapsed / kDuration - floor(elapsed / kDuration);
    self.imageView.transform = CGAffineTransformMakeRotation(M_PI * 2.0 * self.percent);
}

@end