我正在尝试为UIView
(基于块的动画)制作动画,但我无法使计时工作(持续时间等)。动画运行,因为更改已完成,并显示完成输出消息,但它只是忽略持续时间并立即进行更改。
我认为我做事的方式有问题,但我就是不知道错误在哪里。让我解释一下我想要做什么:
我有一个UIViewController
(viewcontroller)来处理UIView
我定义了touchesEnded
方法的子类的两个对象(view1 和 view2)。
这个想法是,当 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;
}
所以......使用我发布的这个简单代码,就会发生错误。正如我所说,动画确实改变了 alpha,但没有延迟或时间。这只是瞬间的。对这些附加信息有任何想法吗?
再次感谢您的帮助。