【发布时间】:2014-02-12 08:05:36
【问题描述】:
在UIViewController 中,我有一个占据屏幕一半的UIScrollView。这个UIScrollView 包含UIView 的集合。在某些特定事件上,例如滑动,我希望我的UIScrollView 以动画方式全屏显示,我该如何实现这种行为?
【问题讨论】:
标签: ios iphone objective-c uiview uiscrollview
在UIViewController 中,我有一个占据屏幕一半的UIScrollView。这个UIScrollView 包含UIView 的集合。在某些特定事件上,例如滑动,我希望我的UIScrollView 以动画方式全屏显示,我该如何实现这种行为?
【问题讨论】:
标签: ios iphone objective-c uiview uiscrollview
试试这个...
// 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 根本没有改变它的大小
handleSwipe 这样添加到事件中,它会起作用。但是有没有办法动画地做到这一点?
[UIView beginbeginAnimations:context:] 和[UIView commitAnimations]
尝试在事件上使用此方法并设置:
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的大小
引用@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];
【讨论】:
我只是扩展了第一个答案,如果您想为上述过程设置动画,可以通过附加以下代码来实现:
- (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");
}];
}
}
【讨论】:
NSLayoutAttributeHeight 更改其高度即可。我知道这种解决方案可能并不典型,但您是否发现任何缺点?