Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use iOS to realize the push-pull effect of imitating Gaode's home page

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use iOS to achieve imitating Gaode home push and pull effect, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The above is the effect of the implementation, and the sliding view is a new UIView subclass

1. The call of sliding view

SlideView * slideView = [[SlideView alloc] initWithFrame:CGRectMake (0, kScreenHeight-140, kScreenWidth, kScreenHeight-100)]; slideView.topH = 100; [self.view addSubview:slideView]

SlideView is a new UIView subclass

KScreenHeight screen height

KScreenWidth screen width

TopH is the distance from the top of the screen when the view slides to the top

Note: the height of SlideView should be the height of the screen minus topH, otherwise the height will be a little inappropriate when the view slides to the top.

2. Add sliding gestures and tableview-related configurations to the view

UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector (panAction:)]; panGestureRecognizer.delegate = self; [self addGestureRecognizer:panGestureRecognizer]; self.tableView.bounces = NO; self.tableView.userInteractionEnabled = NO

TableView must add the above two attributes

The userInteractionEnabled property is used to prevent gestures on the tableview when the view is at the bottom. Not adding this attribute will cause the view to respond to the event that the tableview scrolls up at the bottom, thus preventing the view from sliding up as a whole. When the view slides to the top, you need to set userInteractionEnabled to YES, otherwise tableview cannot scroll up. If the tableview is not shown at the bottom, only some other controls are shown so that you don't need to set this property.

Bounces is set to NO to prevent tableview from responding to its own drop-down events when scrolling to the top, thus responding to the sliding gesture of the entire view.

3. Set to allow multiple gestures to be responded to simultaneously

-(BOOL) gestureRecognizer: (UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {return YES;}

This allows you to respond to multiple gestures at the same time, otherwise the view's gestures will be overwritten by tableview events.

4. Sliding correlation logic processing

1. Get the tableview offset in scrollViewDidScroll and record it.

-(void) scrollViewDidScroll: (UIScrollView *) scrollView {CGFloat currentPostion = scrollView.contentOffset.y; self.stop_y = currentPostion;}

2. Handling of sliding gestures

Self.top is the self.frame.origin.y of the view, which I wrote in the category.

Self.bottomH assigns self.top to self.bottomH at the beginning of the view

The speed and distance are judged during the view sliding, and the view is slid to the bottom and top according to the speed and distance.

-(void) panAction: (UIPanGestureRecognizer *) pan {/ / get view offset CGPoint point = [pan translationInView:self]; / / stop_y is the offset of tableview. When the offset of tableview is greater than 0, the event if (self.stop_y > 0) {/ / resets the video offset to 0 [pan setTranslation:CGPointMake (0,0) inView:self]; return } / / self.top is the distance from the top of the view self.top + = point.y; if (self.top

< self.topH) { self.top = self.topH; } // self.bottomH是视图在底部时距离顶部的距离 if (self.top >

Self.bottomH) {self.top = self.bottomH } / / at the end of the sliding gesture, determine whether the distance between the sliding view and the top is more than half of the screen. If it is more than half, slide down to the bottom / / if less than half, slide up to the top if (pan.state = = UIGestureRecognizerStateEnded | | pan.state = = UIGestureRecognizerStateCancelled) {/ / sliding speed CGPoint velocity = [pan velocityInView:self]; CGFloat speed = 350 If (velocity.y

< -speed) { [self goTop]; [pan setTranslation:CGPointMake(0, 0) inView:self]; return; }else if (velocity.y >

Speed) {[self goBack]; [pan setTranslation:CGPointMake (0,0) inView:self]; return;} if (self.top > kScreenHeight/2) {[self goBack];} else {[self goTop];}} [pan setTranslation:CGPointMake (0,0) inView:self];}

3. Events that slide to the bottom and top

When you slide to the bottom, you need to set userInteractionEnabled to NO and cancel the response event for tableview. Set userInteractionEnabled to YES when you slide to the top

-(void) goTop {[UIView animateWithDuration:0.5 animations: ^ {self.top = self.topH;} completion: ^ (BOOL finished) {self.tableView.userInteractionEnabled = YES;}];}-(void) goBack {[UIView animateWithDuration:0.5 animations: ^ {self.top = self.bottomH;} completion: ^ (BOOL finished) {self.tableView.userInteractionEnabled = NO;}];} 4, attention points

Because NO is set to the serInteractionEnabled property of tableview at the bottom, this causes all events on the tableview to be unchecked, including the selection of cell. If you want to keep this property, you can add [scrollView setContentOffset:CGPointMake (0,0)] to the scrollViewDidScroll.

Comment out all the serInteractionEnabled in the code at the same time.

-(void) scrollViewDidScroll: (UIScrollView *) scrollView {CGFloat currentPostion = scrollView.contentOffset.y; self.stop_y = currentPostion; if (self.top > self.topH) {[scrollView setContentOffset:CGPointMake (0,0)] }} Thank you for reading this article carefully. I hope the article "how to use iOS to achieve the push and pull effect of Gaode's home page" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report