我想動畫UIView
(基於塊的動畫),但我不能使時間工作(持續時間等)。動畫運行,因爲更改已完成,並顯示完成輸出消息,但它只是忽略持續時間並立即進行更改。動畫UIView touchesEnded事件
我認爲我的做事方式有問題,但我無法弄清楚錯誤在哪裏。讓我解釋什麼,我試圖做的:
我有一個UIViewController
(視圖 - 控制),處理一個UIView
sublcass的兩個對象(視圖1和視圖2)在我所定義的touchesEnded
方法。
這個想法是,當view1被觸摸時,我想動畫view2。因此,我所做的是在UIView
子類中實現動畫方法,touchesEnded
方法中的通知(也在UIView
子類中)以及 控制器中的觸發器方法,該方法將調用第二個視圖的動畫。事情是這樣的:
// In the UIView subclass:
- (void) myAnimation{
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.alpha = 0.5;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
// In the UIView subclass:
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event {
[pParent viewHasBeenTouched]; // pParent is a reference to the controller
}
// In the UIViewController:
- (void) viewHasBeenTouched {
[view2 myAnimation];
}
(動畫和工作流程其實有點複雜,但這個簡單的例子,只不過沒有工作)
如果我把動畫其他地方,它可能工作,例如在初始化控制器的init方法中的視圖之後。但是,如果我嘗試執行前向後向調用,動畫將忽略持續時間並在一個步驟中執行。
任何想法?我錯過了關於應該知道的觸摸和手勢的事情?謝謝。
添加到原始發佈新的信息:
的AppDelegate中什麼也不做,但這樣的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] init];
self.window.rootViewController = self.viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
的TestViewControler.h僅僅是這樣的:
#import <UIKit/UIKit.h>
#import "TestView.h"
@interface TestViewController : UIViewController{
TestView* myView;
}
- (void) viewHasBeenTouched;
@end
和viewDidLoad中只做這個:
- (void)viewDidLoad{
[super viewDidLoad];
myView = [[TestView alloc] initWithParent:self];
[self.view addSubview:myView];
}
最後,UIView的有TestView.h它看起來像這樣:
#import <UIKit/UIKit.h>
@class TestViewController; // Forward declaration
@interface TestView : UIView{
TestViewController* pParent;
}
- (id)initWithParent:(TestViewController*) parent;
- (void) myAnimation;
@end
和所使用的init()方法是:
- (id)initWithParent:(TestViewController *)parent{
CGRect frame = CGRectMake(50, 20, 100, 200);
self = [super initWithFrame:frame];
if (self) pParent = parent;
self.backgroundColor = [UIColor blueColor];
return self;
}
所以......這個簡單的代碼我張貼,錯誤發生。正如我所說,動畫確實改變了阿爾法,但沒有延遲或時間。這只是瞬間的。有了這些附加信息的任何想法?
再次感謝您的幫助。
我將你的代碼複製/粘貼到一個項目中,並將「this.alpha」更改爲「self.alpha」(無論如何,你的「this」是什麼?),它工作得很好。 – 2012-02-28 15:03:19
我拼寫'self'爲'this',抱歉(C++遺產)。 – Eduardo 2012-03-01 19:02:15
我嘗試過一個簡單的例子,但是它不起作用。我編輯原始帖子並添加一些關於ViewController的信息,以查看錯誤是否存在。感謝您的幫助和耐心。 – Eduardo 2012-03-01 19:26:56