[英]Rotate NSButton clockwise with animation
我試圖順時針旋轉NSButton,直到用戶手動中斷它為止。 這是我用來完成此任務的代碼。 我知道它曾經在某個時候起作用。 知道如何解決嗎? 提前致謝!
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
a.fromValue = [NSNumber numberWithFloat:0];
a.toValue = [NSNumber numberWithFloat:-M_PI*2];
[self.reloadButton.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
a.duration = 2.0; // seconds
a.repeatCount = HUGE_VAL;
[self.reloadButton.layer addAnimation:a forKey:nil];
只要我設置reloadButton.wantsLayer = YES;
您的代碼就可以正常工作reloadButton.wantsLayer = YES;
在iOS應用中,始終啟用核心動畫,並且每個視圖都有一個圖層支持。 在OS X中,應用程序必須通過執行以下操作顯式啟用Core Animation支持:
嘗試這個
CABasicAnimation *rotation;
rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.fromValue = [NSNumber numberWithFloat:0];
rotation.toValue = [NSNumber numberWithFloat:(M_PI)];
rotation.duration = 0.2; // Speed
rotation.repeatCount = 1; // Repeat forever. Can be a finite number.
[yourButton.layer addAnimation:rotation forKey:@"Spin"];
[yourButton.layer setZPosition:100];
yourButton.transform = CGAffineTransformMakeRotation(M_PI_2);
您可以方便地更改/設置持續時間,repeatCount和toValue。
[EDIT1]
在看到這是用於NS與UI按鈕之后,有兩個選項:
1)為您的OSX版本使用NSTimer和正確的旋轉例程(請參見下面的鏈接)
http://digerati-illuminatus.blogspot.com/2009/09/how-do-you-rotate-nsbutton-nstextfield.html
2)如果您使用的是OSX 10.5或更高版本,則支持CoreAnimation,而NSButton實際上應支持以下內容。
維基鏈接
http://en.wikipedia.org/wiki/Core_Animation
[原始]嘗試使用以下代碼:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5000000];
CGAffineTransform rr=CGAffineTransformMakeRotation(5000000);
reloadButton.transform=CGAffineTransformConcat(reloadButton.transform, rr);
[UIView commitAnimations];
這是顯示兩個方法的SO問題的鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.