当用户开始滚动时,我希望整个UIWebVIEw向上跟随移动直到它到达顶部,然后webvIEw应该开始正常滚动.我希望这发生在同一个手指运动中. (也就是说,用户不需要抬起手指).
我已经在UIWebVIEw上使用UIPanGestureRecognizer实现了这一点(参见下面的代码).手势处理程序移动UIWebVIEw并不断将scrollvIEw的contentOffset重置为0.它可以工作,但性能很差,特别是如果webvIEw包含繁重的页面.我想这是webvIEw的滚动和重置,使渲染变得沉重.我的解决方案闻起来很糟糕,我认为必须有更好的方法吗?
你有没有更好的解决方案来实现我想做的事情?
@interface VIEwController ()@property (strong,nonatomic) IBOutlet UIWebVIEw *webVIEw;@property (nonatomic,strong) UIPanGestureRecognizer *panGestureRecognizer;@property(nonatomic,strong) UIScrollVIEw *webVIEwScrollVIEw;@property (strong,nonatomic) IBOutlet NSLayoutConstraint *webVIEwtopmarginConstraint;@end@implementation VIEwController { BOol gestureEnabled;}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; gestureEnabled = YES; // Initially put the webvIEw 320 px down on the topvIEw self.webVIEw.translatesautoresizingMaskIntoConstraints = NO; self.webVIEwtopmarginConstraint.constant = 320.0; // SET UP GESTURE RECOGNIZER _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesturesForWebvIEw:)]; _panGestureRecognizer.minimumNumberOftouches = 1; _panGestureRecognizer.maximumNumberOftouches = 1; _panGestureRecognizer.delegate = self; [self.webVIEw addGestureRecognizer:_panGestureRecognizer]; // Get hold of the webvIEws scrollvIEw for (UIVIEw* subVIEw in self.webVIEw.subvIEws) { if ([subVIEw isKindOfClass:[UIScrollVIEw class]]) { _webVIEwScrollVIEw = (UIScrollVIEw *)subVIEw; } } // Load a web page in the webvIEw NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.macrumors.com"]]; [self.webVIEw loadRequest:requestObj];}- (voID)handlePanGesturesForWebvIEw:(UIPanGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateChanged) { CGfloat transY = [gesture translationInVIEw:self.vIEw].y; //transY will be negative by the number of pixels panned when panning upwards if (gestureEnabled) { // Move the whole webvIEw according to gesture movement upwards // todo Handle scrolling in the opposite direction self.webVIEwtopmarginConstraint.constant = 320 + transY; if (self.webVIEwtopmarginConstraint.constant <= 0.0) { // The webvIEw is at the top,disable the gesture recognizer gestureEnabled = NO; self.webVIEwtopmarginConstraint.constant = 0.0; self.panGestureRecognizer.delegate = nil; } // "Rewind" the web scrolling CGPoint offset = CGPointMake(0.0,0.0); [self.webVIEwScrollVIEw setContentOffset:offset animated:NO]; } }}#pragma mark UIGestureRecognizerDelegate impl- (BOol)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}@end
编辑:使用@JPHribovsek中的插图在接受的答案中建议完美.代码也变得更简单了.您可以在这里查看解决方案:http://gist.github.com/melke/8684172
解决方法 我认为,在不移动scrollvIEw的情况下,您想要更简单地做的是设置内容插入CGfloat top = 320.0; self.webVIEw.scrollVIEw.contentInset = UIEdgeInsetsMake(top,0.0,0.0);
请注意,您需要将部署目标设置为iOS5才能像这样获取滚动视图,而不是像您一样循环浏览子视图.
总的来说,vIEwDIDLoad方法会更简单,不需要手势识别器:
- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; CGfloat top = 320.0; self.webVIEw.scrollVIEw.contentInset = UIEdgeInsetsMake(top,0.0); self.webvIEw.scrollVIEw.delegate = self; // Load a web page in the webvIEw NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.macrumors.com"]]; [self.webVIEw loadRequest:requestObj];}
此外,根据您在原始帖子中的一条评论,看起来您还在移动另一个视图以及滚动视图.
在这种情况下,您将需要跟踪滚动视图滚动委托,并相应地移动其他视图.根据其他视图的内容(例如,如果它只是一个imageVIEw横幅类型的东西),我个人会发现调整大小/裁剪其他视图而不是将其移出屏幕更为优雅;但这应该是我猜的另一个问题.
- (voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw{ CGfloat offsetY = scrollVIEw.contentOffset.y; //here whatever code you use to set that other vIEw frame position}总结
以上是内存溢出为你收集整理的ios – 滚动时移动整个UIScrollView.在顶部时,开始正常滚动全部内容,希望文章能够帮你解决ios – 滚动时移动整个UIScrollView.在顶部时,开始正常滚动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)