簡體   English   中英

旋轉UIButton

[英]Rotating UIButton

我一直在嘗試使用以下方法旋轉按鈕:

-(IBAction)rotate:(id)sender{
    CGPoint pencilCenter = pencil.center;
    [pencil setCenter:pencilCenter];
    CGFloat floater = 1.0;
    [UIView animateWithDuration:0.7 animations:^(void){
        [pencil setTransform:CGAffineTransformMakeRotation(floater)];
    }];
    [UIView animateWithDuration:0.7 animations:^(void){
        [pencil setTransform:CGAffineTransformMakeRotation(floater)];
    }];
}

這應該使按鈕做某種“搖動”,然后它應該回到原來的位置 - 但它所做的只是改變按鈕的位置,將其移動到只有一側,並在另一個方法的運行中按鈕沒有反應

我的代碼有什么問題?

謝謝!

編輯2:我的問題是 - 我如何制作一個按鈕來做一些搖擺/擺動,例如編輯sptingboard時的擺動應用模式。

使用此代碼可以讓我向左旋轉,從左到右,然后從右到左平滑動畫,然后旋轉到原始位置。 現在,我希望這不僅僅是旋轉,而是通過動畫來實現,就像擺動一樣。

[UIView animateWithDuration:0.25
                      delay:0.0
                    options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse)
                 animations:^ {
                     pencil.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(30));
                     pencil.transform = CGAffineTransformIdentity;
                 }
                 completion:^(BOOL finished){
                 }
 ];

謝謝!

導入“QuartzCore / QuartzCore.h”並嘗試這個,

CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.delegate = self;
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 1.7;
fullRotation.repeatCount = 2;
[btnTemp.layer addAnimation:fullRotation forKey:@"360"];

嘗試使用CGAffineTransformRotate而不是CGAffineTransformMakeRotation 您可以使用`CGAffineTransformIdentity1作為所有調用中的第一個參數,因此根據第二個參數的最終轉換將應用於框架的原始形狀(?)。

雖然它沒有完美地回答這個問題,但Shardul的回答非常適合旋轉按鈕(2個完整旋轉)。 所以在這里,轉換為Swift 3

let fullRotation = CABasicAnimation(keyPath: "transform.rotation")
fullRotation.delegate = self
fullRotation.fromValue = NSNumber(floatLiteral: 0)
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2))
fullRotation.duration = 0.5
fullRotation.repeatCount = 2
button.layer.add(fullRotation, forKey: "360")

您需要導入QuartzCore:

import QuartzCore

並且您的ViewController需要符合CAAnimationDelegate:

class ViewController: UIViewController, CAAnimationDelegate {

}

好吧, 文檔說:“......指定的動畫會立即在另一個線程上啟動......”。 現在,所有UIKit代碼都不是線程安全的。 因此,如果從完成塊(在單獨的線程中運行)調用UI setter(setTransform,setCenter)以使用performSelectorOnMainThread:withObject:調用它們,可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM