Objective c 从superview中删除并启用用户交互
我正在用一种方法制作一些自定义动画来更改视图。我已经使用[UIView setAnimationDidStopSelector:@selectorremoveFromSuperview]从superView中删除了视图,但我还希望在动画结束后启用用户交互 这是我的密码:Objective c 从superview中删除并启用用户交互,objective-c,ios,cocoa-touch,uiviewanimation,Objective C,Ios,Cocoa Touch,Uiviewanimation,我正在用一种方法制作一些自定义动画来更改视图。我已经使用[UIView setAnimationDidStopSelector:@selectorremoveFromSuperview]从superView中删除了视图,但我还希望在动画结束后启用用户交互 这是我的密码: -(void) switchFrom:(UIViewController*) fromViewController To:(UIViewController*) toViewController usingAnimation:(
-(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:
这些按钮在动画过程中仍然是交互式的您需要定义自己的方法,例如删除:并将其作为选择器传递。在方法中,只需使用传递的UIView将其删除
-(void)remove:(id)sender {
UIView *v = (UIView*)sender;
[v removeFromSuperview];
}
您需要定义自己的方法,例如remove:并将其作为选择器传递。在方法中,只需使用传递的UIView将其删除
-(void)remove:(id)sender {
UIView *v = (UIView*)sender;
[v removeFromSuperview];
}
您可以使用以下选项:
[UIView animateWithDuration:0.4
animations:^{ [self performSelector:@selector(removeFromSuperview)]; // other code here}
completion:^(BOOL finished){ [UIView animateWithDuration:0.4
animations:^{ [self performSelector:@selector(setUserInteractionEnabled:)]; //other code here}]; }];
希望对您有所帮助。您可以使用:
[UIView animateWithDuration:0.4
animations:^{ [self performSelector:@selector(removeFromSuperview)]; // other code here}
completion:^(BOOL finished){ [UIView animateWithDuration:0.4
animations:^{ [self performSelector:@selector(setUserInteractionEnabled:)]; //other code here}]; }];
希望对您有所帮助。我认为您可以使用来启用用户交互
self.view.userInteractionEnabled=NO 我认为你可以使用 self.view.userInteractionEnabled=NO 您知道开始忽略交互事件和结束忽略交互事件吗? 通常,如果您不希望在动画过程中进行任何用户交互,则可以使用这些用户交互 无论如何,您仍然需要正确的回调来触发它们。您知道beginIgnoringInteractionEvents和endIgnoringInteractionEvents吗? 通常,如果您不希望在动画过程中进行任何用户交互,则可以使用这些用户交互
无论如何,您仍然需要正确的回调来触发它们。我最终为UIView创建了一个类别扩展…它终于起作用了 UIView+动画.h代码 然后,我在协调控制器中导入了此扩展: C6CoordinatingController.h部分代码 并调用setAnimationDidStopSelector中的选择器: C6CoordinatingController.m部分代码 所以,我最终使用了@jayde3和@Mundi概念,它的效果非常好
谢谢你们 我最终为UIView创建了一个类别扩展…它终于起作用了 UIView+动画.h代码 然后,我在协调控制器中导入了此扩展: C6CoordinatingController.h部分代码 并调用setAnimationDidStopSelector中的选择器: C6CoordinatingController.m部分代码 所以,我最终使用了@jayde3和@Mundi概念,它的效果非常好
谢谢你们 但我需要两个发件人…toView从superView中删除,fromView启用交互。我可以设置两个不同的setAnimationDidStopSelector吗?它会工作吗?从原始superview看不就是吗?如果是这样,为什么不在上面起草的方法中使用该关系?@Till,实际上,有一个mainView superview…我可以尝试这样的方法…但我需要两个发件人…toView从superview中删除,fromView启用交互。我可以设置两个不同的setAnimationDidStopSelector吗?它会工作吗?从原始superview看不就是吗?如果是这样,为什么不在上面起草的方法中使用该关系?@直到,实际上,有一个mainView superview…我可以尝试这样的东西…我做了这个…但我需要它在动画后重新启用。我做了这个…但我需要它在动画后重新启用。不知何故,这段代码不起作用…但它似乎真的在正确的路径上…我将用它做一些测试,谢谢!不知何故,这段代码不起作用…但它似乎真的在正确的道路上…我会用它做一些测试,谢谢!不…我将检查它以了解如何使用它而不是makeInteractionEnabled。它也可以正常工作…但我仍然无法同时执行remove fromView和endIgnoringInteractionEvents…我将尝试创建一个方法,如@Mundi建议删除fromView并调用endIgnoringInteractionEvents。不…我将检查它以了解如何执行使用此方法而不是makeInteractionEnabled。它也可以正常工作…但我仍然无法同时执行remove fromView和endIgnoringInteractionEvents…我将尝试创建一个方法,如@Mundi建议删除fromView并调用endIgnoringInteractionEvents。
#import "UIView+Animated.h"
// 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)];