【问题标题】:iOS - UIScrollView from half to full screeniOS - UIScrollView 从半屏到全屏
【发布时间】:2014-02-12 08:05:36
【问题描述】:

UIViewController 中,我有一个占据屏幕一半的UIScrollView。这个UIScrollView 包含UIView 的集合。在某些特定事件上,例如滑动,我希望我的UIScrollView 以动画方式全屏显示,我该如何实现这种行为?

【问题讨论】:

    标签: ios iphone objective-c uiview uiscrollview


    【解决方案1】:

    试试这个...

    // adding swipe gesture to your scrollview
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    
    // Set swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [scrollview addGestureRecognizer:swipeLeft];
    
    
    // add gesture action method
    - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
    // if you are having only one swipe gesture, you no need to add the below condition. you can add the code inside the condition
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
        scrollView.contentSize = self.view.frame;
        scrollView.frame = self.view.frame;
    }
    }
    

    【讨论】:

    • 不工作,我的UIScrollView 根本没有改变它的大小
    • 我能知道你是如何设置滚动视图的吗?您是使用 xib 还是以编程方式。如果可能的话,你能分享你的代码的 sn-p 吗
    • 相同的代码对我有用..我可以在滑动时重新调整滚动视图的大小..你能检查一下吗。
    • 我只是想测试,所以我在 viewDidLoad 中添加了代码,但由于某种原因它不起作用。但是,如果我尝试像handleSwipe 这样添加到事件中,它会起作用。但是有没有办法动画地做到这一点?
    • 我想我想通了,我使用了[UIView beginbeginAnimations:context:][UIView commitAnimations]
    【解决方案2】:

    尝试在事件上使用此方法并设置:

    scrollView.contentSize = CGSizeMake(847, 800); // change on basis of your requirement
    

    用于动画滚动视图:

    - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;  // animate at constant velocity to new offset
    

    【讨论】:

    • 设置内容偏移不会改变UIScrollView的大小
    • 是的..如果你仔细看,我提到了 contentSize 用于设置和动画我说的是 contentOffset
    【解决方案3】:

    引用@RAJA 的回答并略有改进:

    [UIView beginAnimations:@"whatever" context:nil];
    
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * <count of subviews>);
    scrollView.frame = self.view.frame;
    
    [UIView commitAnimations];
    

    【讨论】:

    • 请按如下方式设置contentsize.. scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * );跨度>
    【解决方案4】:

    我只是扩展了第一个答案,如果您想为上述过程设置动画,可以通过附加以下代码来实现:

    - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe 
     {
       if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) 
        {
        [UIView animateWithDuration:0.15f  // set duration for your animation here
                         animations:^{
    
                             scrollView.contentSize = self.view.frame;
                             scrollView.frame = self.view.frame;
                         }
                         completion:^(BOOL finished){
                             NSLog(@"completion block");
                         }];
    
        }
    }
    

    【讨论】:

    • 我可以使用 NSLayoutConstraint 来做同样的事情,只需使用 NSLayoutAttributeHeight 更改其高度即可。我知道这种解决方案可能并不典型,但您是否发现任何缺点?
    猜你喜欢
    • 2014-05-01
    • 2018-12-20
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多