我正在做一些自定义动画来在一个方法中改变视图。我已经使用[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)]从superView中删除了"fromView“,但我还想在动画结束后启用用户交互。
下面是我的代码:
-(void) switchFrom:(UIViewController*) fromViewController To:(UIViewController*) toViewController usingAnimation:(int) animation{
UIView *fromView = fromViewController.view;
UIView *toView = toViewController.view;
/*************** SET ALL DEFAULT TRANSITION SETTINGS ***************/
// Get the current view frame, width and height
CGRect pageFrame = fromView.frame;
CGFloat pageWidth = pageFrame.size.width;
// Create the animation
[UIView beginAnimations:nil context:nil];
// Create the delegate, so the "fromView" is removed after the transition
[UIView setAnimationDelegate: fromView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
// Set the transition duration
[UIView setAnimationDuration: 0.4];
/*************** IT DOESN'T WORK AT ALL ***************/
[toView setUserInteractionEnabled:NO];
[UIView setAnimationDelegate: toView];
[UIView setAnimationDidStopSelector:@selector(setUserInteractionEnabled:)];
[toView setUserInteractionEnabled:YES];
// Add the "toView" as subview of "fromView" superview
[fromView.superview addSubview:toView];
switch (animation) {
case AnimationPushFromRigh:{
// Position the "toView" to the right corner of the page
toView.frame = CGRectOffset(pageFrame, pageWidth,0);
// Animate the "fromView" to the left corner of the page
fromView.frame = CGRectOffset(pageFrame, -pageWidth,0);
// Animate the "toView" to the center of the page
toView.frame = pageFrame;
// Animate the "fromView" alpha
fromView.alpha = 0;
// Set and animate the "toView" alpha
toView.alpha = 0;
toView.alpha = 1;
// Commit the animation
[UIView commitAnimations];
}
.
.
.你知道如何在setAnimationDidStopSelector中调用这两个方法并让它们协同工作吗?
编辑1
尝试像这样的@safecase代码,替换这个注释代码块:
/*************** IT DOESN'T WORK AT ALL ***************
[toView setUserInteractionEnabled:NO];
[UIView setAnimationDelegate: toView];
[UIView setAnimationDidStopSelector:@selector(setUserInteractionEnabled:)];
[toView setUserInteractionEnabled:YES];
*************** IT DOESN'T WORK AT ALL ***************/
// Remove the interaction
[toView setUserInteractionEnabled:NO];
[fromView setUserInteractionEnabled:NO];
// Create the animation
[UIView animateWithDuration:0.4 animations:^{
[fromView performSelector:@selector(removeFromSuperview)];
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.4
animations:^{
C6Log(@"finished");
[toView performSelector: @selector(setUserInteractionEnabled:)];
}];
}];删除"toView“而不是删除"fromView”:(在动画过程中,按钮将继续交互
发布于 2012-06-14 18:37:46
我最终为UIView…创建了一个类别扩展它终于成功了!
UIView+Animated.h代码
#import <UIKit/UIKit.h>
@interface UIView (Animated)
- (void) finishedAnimation;
@endUIView+Animated.m代码
#import "UIView+Animated.h"
@implementation UIView (Animated)
- (void) finishedAnimation{
C6Log(@"FinishedAnimation!");
[self removeFromSuperview];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
@end然后,我#在我的协调控制器中导入了这个扩展:
C6CoordinatingController.h部分代码
#import "UIView+Animated.h"并在setAnimationDidStopSelector中调用选择器:
C6CoordinatingController.m部分代码
// Create the delegate, so the "fromView" is removed after the transition
[UIView setAnimationDelegate: fromView];
// Ignore interaction events during the animation
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// Set the transition duration
[UIView setAnimationDuration: 0.4];
// Call UIView+Animated "finishedAnimation" method when animation stops
[UIView setAnimationDidStopSelector:@selector(finishedAnimation)];所以,我最终使用了@jaydee3和@芒迪的概念,它就像一个护身符!
谢谢你们!
发布于 2012-06-14 09:47:26
你知道beginIgnoringInteractionEvents和endIgnoringInteractionEvents吗?通常,如果您不想在动画过程中使用任何userInteraction,则可以使用它们。
无论如何,你仍然需要正确的回调来触发它们。
发布于 2012-06-12 20:07:53
您需要定义自己的方法,例如remove:,并将其作为选择器传递。在该方法中,只需使用传递的UIView将其移除。
-(void)remove:(id)sender {
UIView *v = (UIView*)sender;
[v removeFromSuperview];
}https://stackoverflow.com/questions/10996227
复制相似问题