IOS Imitation today's headline slide navigation bar _ios

Source: Internet
Author: User

Before the cloud Habitat Community Platform for you to share the home page navigation package NetEase, NetEase home navigation package optimization, today on the basis of the top two to imitate today's headlines.

1. NetEase Homepage Navigation Package class mainly solves the above navigation ScrollView and the following page ScrollView linkage question, as well as above navigation bar's cheap quantity.

2. NetEase Home Navigation Package optimization is mainly to solve iOS7 above sliding return function in Uiscreenedgepangesturerecognizer and scrollview sliding gesture conflict problem.

Today's copy of today's headline slide navigation and NetEase home navigation package optimization Similar, this is also a resolution gesture conflict, Uipangesturerecognizer and scrollview gestures conflict.

First, the Viewcontroller level

With the above diagram described, the left side of the personal page Viewcontroller above through Addchildviewcontroller added a mainviewcontroller as Rootviewcontroller

Uinavigationcontroller, add Uinavigationcontroller view to the view of the personal page Viewcontroller through Addsubview.

Second, The imitation of today's headline slide navigation has 3 main problems:

1.UIPanGestureRecognizer gestures clash with ScrollView

In order to achieve the drag sliding effect, you need to add a uipangesturerecognizer to the Mainviewcontroller, because the bottom is composed of ScrollView and TableView, So will make uipangesturerecognizer with the bottom of the conflict, and NetEase home navigation package optimization similar to the needs in

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer returns Yes in Othergesturerecognizer to make both Scrolview and Uipangesturerecognizer recognized.

2.UIPanGestureRecognizer slide End position is incorrect

Use Uipangesturerecognizer to change the position of Uinavigationcontroller view based on the amount of cheap At the end of the slide in today's headline, Uinavigationcontroller's view is either in place or on the right, not in the middle, which reminds me of the jelly effect I made before, the gesture state, Change the position of the Uinavigationcontroller view based on the state of the gesture, especially at the end of the gesture.

3. Because 1 makes both Scrolview and Uipangesturerecognizer recognized, the Scrolview also slides when returned

Let the bottom of the Scrolview can slide the time should be Uinavigationcontroller view in the initial position frame of x 0, so in its frame of the x>0, The bottom of the Bottomscrollview scrollenabled=no.

Three, the main implementation code (navigation code will not be affixed, only paste the main)

1. Declare a drag gesture

@property (Nonatomic,strong) Uipangesturerecognizer *pangesturerecognizer;

2. Add gestures for Mainviewcontroller

_pangesturerecognizer=[[uipangesturerecognizer alloc]initwithtarget:self Action: @selector (pangesture:)];
_pangesturerecognizer.delegate=self;

3. Method of gesture recognition

Translation-(void) Pangesture: (uipangesturerecognizer*) pan {//position in view///Cgpoint Point=[pan Locationinview:
Self.navigationController.view];
The amount of movement in view is Cgpoint Point1=[pan TranslationInView:self.navigationController.view] with the finger pressed as the origin point; if (Pan.state==uigesturerecognizerstatechanged&&pan==_pangesturerecognizer) {if (point1.x>0&& self.navigationcontroller.view.frame.origin.x<self.navigationcontroller.view.frame.size.width-100) {float x= Self.navigationcontroller.view.frame.origin.x+point1.x>self.navigationcontroller.view.frame.size.width-100?
self.navigationcontroller.view.frame.size.width-100:self.navigationcontroller.view.frame.origin.x+point1.x; Self.navigationcontroller.view.frame=cgrectmake (x, SELF.NAVIGATIONCONTROLLER.VIEW.FRAME.ORIGIN.Y,
Self.navigationController.view.frame.size.width, Self.navigationController.view.frame.size.height);
NSLog (@ "%@", Nsstringfromcgrect (Self.navigationController.view.frame)); else if (point1.x<0) {NSLog (@ "AAA%f", Self.navigationcontroller.view.frame.origin.x); Float x=self.navigationcontroller.view.frame.origin.x+point1.x<0?0:
self.navigationcontroller.view.frame.origin.x+point1.x; Self.navigationcontroller.view.frame=cgrectmake (x, SELF.NAVIGATIONCONTROLLER.VIEW.FRAME.ORIGIN.Y,
Self.navigationController.view.frame.size.width, Self.navigationController.view.frame.size.height); } else if (pan.state==uigesturerecognizerstateended) {if (self.navigationcontroller.view.frame.origin.x>
SELF.NAVIGATIONCONTROLLER.VIEW.FRAME.SIZE.WIDTH/3) {[UIView animatewithduration:0.2 animations:^{ Self.navigationcontroller.view.frame=cgrectmake (self.navigationcontroller.view.frame.size.width-100, 0,
Self.navigationController.view.frame.size.width, Self.navigationController.view.frame.size.height);
} completion:^ (BOOL finished) {self.bottomscrollview.scrollenabled=yes;}]; else {[UIView animatewithduration:0.2 animations:^{self.navigationcontroller.view.frame=cgrectmake (0, 0, Self.navigationcontroller.view.frame.size.wIdth, Self.navigationController.view.frame.size.height);
} completion:^ (BOOL finished) {self.bottomscrollview.scrollenabled=yes;}];
}//Offset is added should be set to 0 [pan Settranslation:cgpointzero InView:self.navigationController.view]; }

4. Gesture Conflict Resolution

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer
{
if ( Self.bottomscrollview.contentoffset.x<=0.0&&gesturerecognizer==_pangesturerecognizer) {
if ( self.navigationcontroller.view.frame.origin.x>0) {
self.bottomscrollview.scrollenabled=no;
}
else
{
self.bottomscrollview.scrollenabled=yes;
}
Return YES
}
return NO;
}

Four, the effect chart

The above is a small set to share the iOS imitation of today's headline slide navigation bar, hope to help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.