iOS interface-Imitation NetEase news left-hand drawer Interactive

Source: Internet
Author: User

1. Introduction

The students who used NetEase news client will find that when NetEase news slides to the left, the left navigation bar will then be dragged out, and the news content list will be pulled to the far right. Like a drawer pulled out the same. Cool. In addition to NetEase news, many applications are now using this kind of interaction.

For unfamiliar gesture recognition please refer to the previous article: detailed use of iOS gesture recognition (drag, zoom, rotate, click, gesture dependency, custom gestures)

This interactive effect mainly uses two gestures, one is pan drag, one is tap. Drag to pull the drawer out, and then push back. Click to push the drawer back.

The effect is as follows:

So how is this effect implemented?

2. Realization of ideas and steps

Ideas: from the implementation of the effect analysis, can be implemented as follows:

This interaction is composed of two view, the left navigation view is below, the view of the table of contents is above, the view of the table of contents covers the Navigation view, the view of the content list is dragged to the right, and the Navigation view is displayed.

Implementation steps:

    1. Customize a view, which is the view that displays the content. Add two gestures to this view, pan drag, tap tap.
    2. When dragging the view, let View.center move to the right so that you can see the content view move to the right.
    3. Define a drawer open stop when the X value is: Opencenterx, this is where the content view finally stops
    4. When the content view crosses an X-value in the middle right, the view automatically moves to the right to stop.
    5. When the content view is in the open state, click on the content view and use the UIView animation to move the content view.center back to the middle.
    6. Set the shadow effect of a content view

3. Code implementation

New CustomView Inheritance UIView

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. #import <UIKit/UIKit.h>
    2. @interface Customview:uiview
    3. {
    4. Cgpoint Openpointcenter;
    5. Cgpoint Closepointcenter;
    6. }
    7. -(ID) Initwithview: (uiview*) Contentview Parentview: (uiview*) Parentview;
    8. @property (nonatomic, strong) UIView *parentview; The parent view of the//drawer view
    9. @property (nonatomic, strong) UIView *contenview; //Drawer display view of content
    10. @end

Two gestures are implemented in custom and pass through the content view and parent view at initialization time.

[CPP]View Plaincopy
  1. #import "CustomView.h"
  2. #define OPENCENTERX 220.0
  3. #define Dividwidth 70.0//opencenterx corresponds to the demarcation line confirming whether to open or close.
  4. @implementation CustomView
  5. -(ID) initWithFrame: (CGRect) frame
  6. {
  7. self = [super Initwithframe:frame];
  8. if (self) {
  9. //initialization code
  10. }
  11. return self;
  12. }
  13. -(ID) Initwithview: (UIView *) Contentview Parentview: (UIView *) Parentview
  14. {
  15. self = [Super Initwithframe:cgrectmake (0,0,contentview.frame.size.width, contentview.frame.size.height)];
  16. if (self) {
  17. Self.contenview = Contentview;
  18. Self.parentview = Parentview;
  19. [Self addsubview:contentview];
  20. Uipangesturerecognizer *pangesturerecognizer = [[Uipangesturerecognizer alloc]
  21. Initwithtarget:self
  22. Action: @selector (Handlepan:)];
  23. [Self addgesturerecognizer:pangesturerecognizer];
  24. UITapGestureRecognizer *tapgesturerecognizer = [[UITapGestureRecognizer alloc]
  25. Initwithtarget:self
  26. Action: @selector (Handletap:)];
  27. [Self addgesturerecognizer:tapgesturerecognizer];
  28. Openpointcenter = Cgpointmake (self.parentview.center.x + Opencenterx,
  29. SELF.PARENTVIEW.CENTER.Y);
  30. NSLog (@"Openpointcenter x:%f, Openpointcenter y:%f",
  31. Openpointcenter.x,
  32. OPENPOINTCENTER.Y);
  33. }
  34. return self;
  35. }
  36. -(void) Handlepan: (uipangesturerecognizer*) Recognizer
  37. {
  38. Cgpoint translation = [recognizer TranslationInView:self.parentView];
  39. float x = self.center.x + translation.x;
  40. NSLog (@"translation x:%f", translation.x);
  41. if (x < self.parentview.center.x) {
  42. x = self.parentview.center.x;
  43. }
  44. Self.center = Cgpointmake (x, OPENPOINTCENTER.Y);
  45. if (recognizer.state = = uigesturerecognizerstateended)
  46. {
  47. [UIView animatewithduration:0.75
  48. delay:0.01
  49. Options:uiviewanimationcurveeaseinout
  50. animations:^ (void)
  51. {
  52. if (x > Openpointcenter.x-dividwidth) {
  53. Self.center = Openpointcenter;
  54. }else{
  55. Self.center = Cgpointmake (Openpointcenter.x-opencenterx,
  56. OPENPOINTCENTER.Y);
  57. }
  58. }completion:^ (BOOL isfinish) {
  59. }];
  60. }
  61. [Recognizer Settranslation:cgpointzero InView:self.parentView];
  62. }
  63. -(void) Handletap: (uitapgesturerecognizer*) Recognizer
  64. {
  65. [UIView animatewithduration:0.75
  66. delay:0.01
  67. Options:uiviewanimationtransitioncurlup animations:^ (void) {
  68. Self.center = Cgpointmake (Openpointcenter.x-opencenterx,
  69. OPENPOINTCENTER.Y);
  70. }completion:nil];
  71. }
  72. @end

4. Call of Viewcontroller

In order to implement the shadow of a custom view, add the need to use the Quartzcore framework. The header file is introduced after the Quartzcore framework is added to the project.

[CPP]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. CGRect rect = CGRectMake (0, 0,
  5. Self.view.frame.size.width,
  6. Self.view.frame.size.height);
  7. NSLog (@"w:%f, h:%f", Rect.size.width, Rect.size.height);
  8. Uiimageview *imageleft = [[Uiimageview alloc]initwithimage:[uiimage imagenamed:@"Left.png"]];
  9. Imageleft.frame = rect;
  10. [Self.view Addsubview:imageleft];
  11. UIView *contentview = [[UIView alloc] initwithframe:rect];
  12. Uiimageview *imageview = [[Uiimageview alloc]initwithimage:[uiimage imagenamed:@"Index.png"]];
  13. Imageview.frame = rect;
  14. [Contentview Addsubview:imageview];
  15. CustomView *customview = [[CustomView alloc] Initwithview:contentview
  16. ParentView:self.view];
  17. [[CustomView Layer] Setshadowoffset:cgsizemake (10, 10)];
  18. [[CustomView layer] setshadowradius:20];
  19. [[CustomView layer] setshadowopacity:1];
  20. [[CustomView layer] setshadowcolor:[uicolor Blackcolor]. Cgcolor];
  21. [Self.view Addsubview:customview];
  22. }

In order to look good, I made two, one is the content view, one is the view of the left navigation bar, and then as the background map in the above two view.

So do not point inside the content, can not drop, that is the picture just. This is just the effect of the demo drawer.

Finally, NetEase news this interaction can pull out from the right side of the effect, the principle is similar, may need more than one view. There are also interactive when the left column and vhts darkened, suddenly large and small effect. These later have time to realize again.

Sequel:iOS interface-Imitation NetEase news left-hand drawer interactive continuation (Add news content page and Comment page gesture)

CSDN Download: Code download

Github:https://github.com/schelling/neteasenews

iOS interface-Imitation NetEase news left-hand drawer Interactive

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.