【问题标题】:presentModalViewController in CGRectCGRect 中的 presentModalViewController
【发布时间】:2012-03-31 19:08:59
【问题描述】:

是否可以以将模态视图限制在 CGRect 中包含的空间的方式呈现模态视图控制器?

如果不是,请说明如何在两个视图之间复制交叉溶解模态视图转换。

谢谢。

【问题讨论】:

    标签: iphone objective-c ios modalviewcontroller presentmodalviewcontroller


    【解决方案1】:

    要交叉溶解到常规视图控制器,您可以将其 modalTransitionStyle 设置为 UIModalTransitionStyleCrossDissolve 然后以模态方式呈现。

    要在一对子视图之间执行交叉融合(仅限于它们的框架 CGRects),您可以使用这个 UIView 方法:

    + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion.
    

    您可以在代码中使用它:

    @interface ViewController ()
    
    @property(strong,nonatomic) UIView *redView;
    @property(strong,nonatomic) UIView *blueView;
    
    @end
    
    @implementation ViewController
    
    @synthesize redView=_redView;
    @synthesize blueView=_blueView;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        self.redView = [[UIView alloc] initWithFrame:CGRectMake(40.0, 40.0, 240.0, 100.0)];
        self.redView.backgroundColor = [UIColor redColor];
        [self.view addSubview:self.redView];
    
        self.blueView = [[UIView alloc] initWithFrame:CGRectMake(40.0, 40.0, 240.0, 100.0)];
        self.blueView.backgroundColor = [UIColor blueColor];
    }
    
    - (IBAction)crossDisolve:(id)sender {
    
        UIView *fromView = (self.redView.superview)? self.redView : self.blueView;
        UIView *toView = (fromView==self.redView)? self.blueView : self.redView;
    
        [UIView transitionFromView:fromView
                        toView:toView
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {NSLog(@"done!");}
     ];
    
     // now the fromView has been removed from the hierarchy and the toView has been added
     // please note that this code depends on ARC to release objects correctly
    
    }
    

    您的问题中较难的部分是使新的子视图“模态”的想法,我猜您的意思是它仅涵盖显示的一部分,但仅关注输入。与 SDK 中最接近的是 UIAlertView。

    【讨论】:

    • 非常感谢。你能举一个简单的例子来说明它在子视图中的使用吗?
    • 你的意思是 UIView 过渡?当然,会掀起一些未经超级测试的东西并编辑我的答案。
    • 再次感谢。对于我的具体情况,由于其简单性,我发现挤在您上面的答案更有帮助。我希望我可以将两个答案都标记为正确,但我一定会立即投票赞成这个答案。再次感谢。
    • @user817946,不用担心。如果您不打算更新您的视图层次结构,它会更简单,这似乎是您在问题中寻找的。但是,如果您只是想为 alpha 之类的视图属性设置动画,请使用苹果推荐的更简单的块动画:[UIView animateWithDuration:0.5 animations:^{ view.alpha = 1.0; }];
    【解决方案2】:

    你只使用 UIView 动画怎么样:

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(x,y,w,h)];
    [view setAlpha:0];
    [self.view addSubView:view];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [view setAlpha:1];
    [UIView commitAnimations];
    

    瞧!它消失了! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多