iOS Development drawer Effect encapsulation use

Source: Internet
Author: User
<span id="Label3"></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">DragerViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Screenw [uiscreen Mainscreen].bounds.size.width<span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Dragerviewcontroller ()</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* < #注释 #></span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@property (nonatomic, weak) UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">leftv, @property (nonatomic, weak) UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">rightv, @property (nonatomic, weak) UIView</span></span>*<span style="color: #000000;"><span style="color: #000000;">mainv;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Dragerviewcontroller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1: Adding child controls</span></span><span style="color: #000000;"><span style="color: #000000;">[self setUp]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2: Add a drag-and-drop panning gesture</span></span>Uipangesturerecognizer *pan =<span style="color: #000000;"><span style="color: #000000;">[[uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)]; [SELF.MAINV addgesturerecognizer:pan]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3: Add a tap gesture to the Controller's view: to add gestures to the controller, the controller responds to events regardless of whether the top view or the bottom view is Clicked. </span></span>UITapGestureRecognizer *tap =<span style="color: #000000;"><span style="color: #000000;">[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (tap)]; [self.view addgesturerecognizer:tap];}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark-tap the event to reset the drawer-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) tap{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Let MAINV reset</span></span><span style="color: #000000;"><span style="color: #000000;">[UIView animatewithduration:</span></span><span style="color: #800080;"><span style="color: #800080;">0.5</span></span>animations:^<span style="color: #000000;"><span style="color: #000000;">{self.mainV.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.view.bounds; }];}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark-drag the pan gesture event<span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* 1: like # define and static can be defined above the @implementation, or under the definition can be</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>TARGETR 275<span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>targetl-275-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Pan: (uipangesturerecognizer *<span style="color: #000000;"><span style="color: #000000;">) pan{</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* 1: Why not use transform, is because we have to modify the height, using transform, can only modify, x, y, cumulative deformation (y direction does not need to translate) Self.mainV.transform = cgaffinetransfor Mtranslate (self.mainV.transform, transp.x, 0); 2: Modify the Control's frame:1: you can use frame to modify the 2: you can use transform to modify, rotate, pan, zoom, accumulate and non-cumulative deformation, empty the deformation, type Cgaffinetransform type 3: the method of modifying frame is encapsulated, An X value is passed to return the value of a modified Frame. * </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1: Get offset</span></span>Cgpoint TRANSP =<span style="color: #000000;"><span style="color: #000000;">[pan translationInView:self.mainV]; Self.mainV.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">[self framewithoffsetx:transp.x]; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * 1: the X-value determines whether to shift to pan left or right: greater than 0 to the right, and less than 0 to pan to the left. Show and hide to control which view 2:view to add the order, Mainview on the top, all added to the Self.view *</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2: determine the direction of the drag, show the different view</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.mainv.frame.origin.x ><span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Right</span></span>Self.rightV.hidden =<span style="color: #000000;"><span style="color: #000000;">YES; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.mainv.frame.origin.x <<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">left</span></span>Self.rightV.hidden =<span style="color: #000000;"><span style="color: #000000;">NO; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3: when the finger is released, do the automatic positioning.</span></span>CGFloat target =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(pan.state = =<span style="color: #000000;"><span style="color: #000000;">Uigesturerecognizerstateended) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.mainv.frame.origin.x > Screenw *<span style="color: #800080;"><span style="color: #800080;">0.5</span></span><span style="color: #000000;"><span style="color: #000000;"> ) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1 Judging on the right</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">current View's x is greater than half the width of the screen, greater than the right side</span></span>target =<span style="color: #000000;"><span style="color: #000000;">targetr; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(cgrectgetmaxx (self.mainV.frame) < Screenw *<span style="color: #800080;"><span style="color: #800080;">0.5</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Judging on the left</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">largest x of the current view is less than half the width of the screen, less than the left</span></span>target =<span style="color: #000000;"><span style="color: #000000;">targetl; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">calculates the current MAINV Frame.</span></span>CGFloat offset = target-<span style="color: #000000;"><span style="color: #000000;">self.mainv.frame.origin.x; [UIView animatewithduration:</span></span><span style="color: #800080;"><span style="color: #800080;">0.5</span></span>animations:^<span style="color: #000000;"><span style="color: #000000;">{self.mainV.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">[self framewithoffsetx:offset]; }]; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4: Reset: The purpose of the reset operation is to make it translate based on the last translation instead of translating it each time according to the initial position</span></span><span style="color: #000000;"><span style="color: #000000;">[pan Settranslation:cgpointzero inView:self.mainV]; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Maxy 100<span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark-calculates Mainv's frame-based on offset<span style="color: #000000;"><span style="color: #000000;">(cgrect) framewithoffsetx: (cgfloat) OffsetX {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">when dragging and panning, change the x value and height</span> of the Mainview</span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * 1:X value is cumulative deformation, so is frame.origin.x + = OffsetX 2: The maximum y value, set the maximum y value of 100, with the current x value multiplied by the maximum Y value and screen width ratio 3: Drag the height of the translation: screen height Degrees-twice times the maximum y value, and returns the frame 4:fabs: absolute value of the calculation result, because when the left slide, the X value is negative</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1:x Value</span></span>CGRect frame =<span style="color: #000000;"><span style="color: #000000;">self.mainV.frame; Frame.origin.x</span></span>+=<span style="color: #000000;"><span style="color: #000000;">offsetX; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">when the x value of the dragged view equals the screen width, the Maxy is the largest and the maximum is</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">375 * 100/375 =</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">absolute value of the result of the calculation</span></span>CGFloat y = fabs (frame.origin.x * maxy/<span style="color: #000000;"><span style="color: #000000;">screenw); FRAME.ORIGIN.Y</span></span>=<span style="color: #000000;"><span style="color: #000000;">y; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2: The height of the screen minus twice times the Y value</span></span>Frame.size.height = [uiscreen mainscreen].bounds.size.height-(<span style="color: #800080;"><span style="color: #800080;">2</span></span>*<span style="color: #000000;"><span style="color: #000000;">frame.origin.y); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">frame;}</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* Add view: The view at this time is decorated with attributes, decorated with weak, SELF.LEFTV = leftv, can be written before or after addsubview, can ensure that the object weak point will not be destroyed. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) setup{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">LEFTV</span></span>UIView *LEFTV =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] initWithFrame:self.view.bounds]; Leftv.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor bluecolor]; SELF.LEFTV</span></span>=<span style="color: #000000;"><span style="color: #000000;">leftv; [self.view addsubview:leftv]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Rightv</span></span>UIView *rightv =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] initWithFrame:self.view.bounds]; Rightv.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor greencolor]; Self.rightv</span></span>=<span style="color: #000000;"><span style="color: #000000;">rightv; [self.view addsubview:rightv]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Mianv</span></span>UIView *MAINV =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] initWithFrame:self.view.bounds]; Mainv.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor redcolor]; SELF.MAINV</span></span>=<span style="color: #000000;"><span style="color: #000000;">mainv; [self.view addsubview:mainv];}</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* Thought summary: 1: concrete idea: 1: Custom controller, add left and right drawers and Mainview on the Controller's view, add panning gesture to mainview, Add a tap gesture to self.view, Although the click is Mainview or other view, but the response event is Self.view 2:1: in the method of panning gestures, the distance to translate, at this time to change the Mainview frame there are two ways, Transform add deformation or change frame directly, use transform cannot set its height, so use change frame method. Wraps the method that changes the frame to enclose the external input offset x value, returns a Mainview frame. 2:1: sets the x value of the frame, the x value is the cumulative deformation, frame.origin.x + = offsetX, and calculates the Y value and calculates the height based on the scale of the x-value translation, cgfloat y = fabs (frame.origin.x * MAXY/SCREENW FRAME.ORIGIN.Y = y, because you want to consider panning left or right so take absolute values, set the Y value, and calculate the height to return to the Frame. 3: in accordance with the judge left or right to show or hide the drawer, (judging by the X-plus or Minus) 4: then the finger left automatically reset, according to the X value and the maximum x value and the screen width of half to compare, to pass in the X value set frame, and finally the only 0, in order to calculate based on the last translation 3: Click on the drawer or mainview to restore it. Just change the frame of Mainview directly.</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p>Second: the use of drawer effect:</p></p><p><p>1: When we use other people to encapsulate the framework, if not not meet the requirements of the source code should not be modified, the use of inheritance to take advantage of others encapsulated framework</p></p><p><p></p></p><p><p>2: when we have dragged the control into the xib or storyboard, we have created a new class that will be associated with the xib or the dragged control in the storyboard, which can be set in Xib or storyboard at this time:</p></p><p><p></p></p><p><p>3: for the left and right drawer page business logic processing generally do not give view processing, complex business logic to the controller to deal with, so the left and right drawer view is added to the controller view, and when the Controller's view is a parent-child relationship, then control its device should also be mutual parent-child relationship</p></p><p><p>4: when we go to encapsulate a frame, we do not want the outside to change the exposed interface, can be decorated with the ReadOnly Property. It is not possible to use self to access the property variable in its own class. m can only use the underscore member variable to assign a Value. And the outside can neither use self to access the underlined member variable, nor can it be accessed with an underscore member Variable.</p></p><p><p></p></p><p><p></p></p><p><p>iOS Development drawer Effect encapsulation use</p></p></span>

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.