【问题标题】:UIImageView Rotation Animation using CADisplayLink - Tracking Rotation使用 CADisplayLink 的 UIImageView 旋转动画 - 跟踪旋转
【发布时间】:2015-12-02 23:09:50
【问题描述】:

有人建议我尝试使用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];
 }

【问题讨论】:

    标签: ios uiimageview rotation cadisplaylink


    【解决方案1】:

    您可以只使用属性来跟踪旋转的状态,这样当您停止它时,它可以从停止的地方继续。例如:

    @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
    

    【讨论】:

    • 看起来很有道理。它也是如此。每 2.5 秒 360 度旋转?并无限旋转直到停止?如果我的 UIImageView 在我的 Storyboard 上并链接到我的.h,这会起作用吗?抱歉,这是我第一次接触这种东西。此外,在这种情况下,我之前没有见过问号语法。来晚了,我明天好好看看。
    • 哦等等,这很快吗?
    • 糟糕,对此我深表歉意。感谢您花时间发帖,我很感激。明天我会试着把它翻译成 ObjC。
    • 翻译很简单。请参阅上面的修订答案。
    • 非常感谢。我迫不及待地想明天试一试(这里几乎是凌晨 1 点)。我会告诉你进展如何(:再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多