2012-02-27 64 views
1

我想动画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; 
} 

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

再次感谢您的帮助。

+0

我将你的代码复制/粘贴到一个项目中,并将“this.alpha”更改为“self.alpha”(无论如何,你的“this”是什么?),它工作得很好。 – 2012-02-28 15:03:19

+0

我拼写'self'为'this',抱歉(C++遗产)。 – Eduardo 2012-03-01 19:02:15

+0

我尝试过一个简单的例子,但是它不起作用。我编辑原始帖子并添加一些关于ViewController的信息,以查看错误是否存在。感谢您的帮助和耐心。 – Eduardo 2012-03-01 19:26:56

回答

1

我发现了这个问题。我想首先为所有人道歉,如何研究我的问题,并在其上浪费宝贵的时间。

我在原始问题中张贴的第二部分确实是复制并粘贴的,它是第一部分有这两个不匹配的错误,因为它们来自更复杂的代码。正如你所说的那样,这些代码没有问题。问题是,我没有复制和粘贴,我以为我已经从项目中删除一个方法(被称为在应用程序的开始):

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

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

再次非常感谢你的时间,我真诚的道歉。