【问题标题】:Animate UIView on touchesEnded event在 touchesEnded 事件上为 UIView 设置动画
【发布时间】:2012-02-27 09:39:03
【问题描述】:

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

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

我有一个 UIViewController (viewcontroller) 来处理 UIView 子类的两个对象(view1 和 view2),我在其中定义了 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;
}

所以...使用我发布的这个简单代码,就会发生错误。正如我所说,动画确实改变了 alpha,但没有延迟或时间。这只是瞬间的。对这些附加信息有任何想法吗?

再次感谢您的帮助。

【问题讨论】:

  • 我将您的代码复制/粘贴到一个项目中,并将“this.alpha”更改为“self.alpha”(您的“this”到底是什么?)它工作得很好。
  • 我把self 拼错为this,对不起(c++ 遗产)。
  • 我尝试了一个和我发布的一样简单的示例,但它不起作用。我编辑了原始帖子并添加了有关 ViewController 的更多信息,以查看是否存在错误。感谢您的帮助和耐心。
  • 我再次将您的代码粘贴到我的项目中,它工作正常。您能否创建一个新项目并插入您在此处发布的内容并进行报告。顺便说一句:你有任​​何警告吗?你在用什么设备?你用的是什么编译器?你用的是什么SDK?...

标签: ios animation uiview duration touches


【解决方案1】:

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

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

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

显然不需要进一步解释。

再次非常感谢您抽出宝贵时间,并诚挚地向您道歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多