2014-02-12 28 views
0

UIViewController中,我有一個UIScrollView,它佔用了屏幕的一半。此UIScrollView包含UIView的集合。在某些特定事件(例如滑動)上,我希望我的UIScrollView能夠全屏動畫,我該如何實現這一行爲?iOS - 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; 
} 
} 
+0

不工作,我的'UIScrollView'根本不改變它的大小 – bohanl

+0

我可以知道你如何設置滾動視圖。你是用xib還是以編程方式。如果可能的話,你可以分享你的代碼片段 – RAJA

+0

相同的代碼,它爲我工作..我能夠重新大小滾動查看滑動..你可以請檢查它。 – RAJA

0

嘗試使用這種方法的事件,並設置:

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 
+0

設定內容偏移不會改變'UIScrollView' – bohanl

+0

的大小是的..如果你仔細看看我已經提到contentSize設置和動畫我已經說contentOffset –

0

從@ 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]; 
+0

請設置內容如下。scrollview.contentSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height * <子視圖的數量>); – RAJA

+0

感謝您指出這一點。完成 – bohanl

0

我只是延長了第一個答案,如果你想動畫上述過程,就可以實現通過追加下面的代碼:

- (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"); 
        }]; 

    } 
} 
+0

我只能通過使用NSLayoutAttributeHeight改變它的高度來使用NSLayoutConstraint。我知道這個解決方案可能不是典型的,但你是否看到任何缺點? – bohanl