1

我正在尝试为UIView(基于块的动画)制作动画,但我无法使计时工作(持续时间等)。动画运行,因为更改已完成,并显示完成输出消息,但它只是忽略持续时间并立即进行更改。

我认为我做事的方式有问题,但我就是不知道错误在哪里。让我解释一下我想要做什么:

我有一个UIViewController(viewcontroller)来处理UIView我定义了touchesEnded方法的子类的两个对象(view1 和 view2)。

这个想法是,当 view1 被触摸时,我想为 view2 设置动画。所以我所做的是在子类中实现一个动画方法,在方法中(也在子类中)实现UIView一个通知,以及在控制器中调用第二个视图的动画的触发方法。像这样的东西:touchesEndedUIView

// 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,但没有延迟或时间。这只是瞬间的。对这些附加信息有任何想法吗?

再次感谢您的帮助。

4

1 回答 1

1

我发现了问题。我想首先向所有研究我的问题并为此浪费宝贵时间的人道歉。

我在原始问题中发布的所有第二部分实际上都是复制和粘贴的,这是第一部分存在这两个不匹配错误,因为它们来自更复杂的代码。正如你们所说的那样,该代码可以毫无问题地工作。问题是我没有复制和粘贴我认为已从项目中删除的方法(并且在应用程序开始时调用):

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation {
    // ... More code
    [UIView setAnimationsEnabled:NO];
    // ... More code
    return YES;
}

这显然不需要进一步解释。

再次非常感谢您的宝贵时间,我真诚地道歉。

于 2012-03-03T10:26:49.810 回答