IOS Custom Swipe back gesture

Source: Internet
Author: User

/**

Just change to the following code in your custom navigation controller, and the custom gesture returns

*/

#define Key_window [[UIApplication sharedapplication] Keywindow]

#define KSCREENW KEY_WINDOW.bounds.size.width

#define Kanimateduration 0.3

#define KDEFAULTALAPA 0.5

#define Kdefaultscale 0.95

#define IOS7 ([Uidevice currentdevice].systemversion.floatvalue >= 7.0)

#import "CXNavigationController.h"

#import <QuartzCore/QuartzCore.h>

@interface Cxnavigationcontroller ()

{

Nsmutablearray *_images; // Image Array

UIView *_bgview; // Background Map

UIView *_blankmask; // Masks

Uiimageview *_imageview; // Image

cgpoint _startpoint; // start point of contact

}

@end

@implementation Cxnavigationcontroller

-(void) viewdidload{

[Superviewdidload];

_images = [nsmutablearrayarray];

if (! iOS7) {

uipangesturerecognizer *pan = [[uipangesturerecognizeralloc] initwithtarge T:selfAction:@selector(panmove:)];

[self. ViewAddgesturerecognizer:p an];

}

}

-(void) Panmove: ( Uipangesturerecognizer *) pan{

if (self.) viewcontrollers. Count <= 1) {

return;

}

cgrect Selff = self. View. frame;

cgpoint location = [Pan locationinview:Key_window];

if (pan. State == Uigesturerecognizerstatebegan) { // start dragging

_startpoint = location;

_imageview. Image = [_imageslastobject];

[key_windowinsertsubview:_bgviewbelowsubview:Self c13>. Tabbarcontroller . View ];

_imageview. Transform = Cgaffinetransformmakescale(kdefaultscale, kdefaultscale);

}Elseif(pan. State == Uigesturerecognizerstatechanged) { // drag in

cgfloat Marginx = location. x - _startpoint. x;

Marginx = marginx > kscreenw ? kscreenw : Marginx;

Marginx = Marginx < 0 ? 0 : Marginx;

Selff. Origin. x = Marginx;

Self. View. frame = Selff;

cgfloat scale = Kdefaultscale + marginx * 1.0 / 6400;

Scale = Marginx = = kscreenw ? 0.5 : scale;

Scale = Marginx = = 0 ? 0 : scale;

_imageview. Transform = Cgaffinetransformmakescale(scale, scale);

cgfloat alpha = Kdefaultalapa -(1 - kdefaultalapa) * Marginx * 1.0 / kscreenw;

Alpha = Marginx = = kscreenw ? Kdefaultalapa : Alpha;

Alpha = Marginx = = 0 ? 0 : Alpha;

_blankmask. Alpha = Alpha;

}Elseif(pan. State == uigesturerecognizerstateended) { // End

cgfloat Marginx = location. x - _startpoint. x;

if (Marginx > ) {

Marginx = marginx > kscreenw ? kscreenw : Marginx;

cgfloat dur = kanimateduration -Marginx * 1.0 / ;

[UIViewanimatewithduration:d ur animations: ^{

cgrect Selff = self. View. frame;

Selff. Origin. x = Key_window. bounds. size. width;

Self. View. frame = Selff;

_blankmask. Alpha = 0;

_imageview. Transform = Cgaffinetransformmakescale(1, 1);

} Completion: ^ (BOOL finished) {

[_images removelastobject];

[_bgview removefromsuperview];

cgrect rect = self. View. frame;

Rect.Origin. x = 0;

Self. View. frame = rect;

[Superpopviewcontrolleranimated:NO];

}];

}Else{

cgfloat Marginx = location. x - _startpoint. x;

Marginx = marginx > ? : Marginx;

Marginx = Marginx < 0 ? 0 : Marginx;

cgfloat dur = Marginx * 1.0 / ;

[UIViewanimatewithduration:0.1 + dur animations: ^ {

cgrect Selff = self. View. frame;

Selff. Origin. x = 0;

Self. View. frame = Selff;

}];

}

}

}

-(void) Pushviewcontroller: (uiviewcontroller *) Viewcontroller animated: (BOOL) animated{

if (self. Viewcontrollers. Count > 0 ) {

if (! iOS7) {

[ self createbg];

}

}

[Super pushviewcontroller: Viewcontroller animated: animated];

}

#pragma mark Create a background

-(void) createbg{

[_imagesaddobject: [self currentscreenimage]];

if (! _bgview) {

_bgview = [[UIView alloc] initwithframe:key_window. Bounds];

_bgview. BackgroundColor = [uicolorblackcolor];

_imageview = [[uiimageview alloc] initwithframe:key_window. Bounds];

_imageview. BackgroundColor = [uicolorclearcolor];

[_bgviewaddsubview:_imageview];

_blankmask = [[UIView alloc] initwithframe:key_window. Bounds];

_blankmask. BackgroundColor = [uicolorblackcolor];

_blankmask. Alpha = Kdefaultalapa;

[_bgviewaddsubview:_blankmask];

}

}

#pragma mark Gets the current screen

-(UIImage *) currentscreenimage{

uigraphicsbeginimagecontextwithoptions(key_window. bounds . size , Self . View . Opaque , 0.0 );

if (self. Topviewcontroller. Tabbarcontroller) {

[self. Topviewcontroller. Tabbarcontroller. View. Layerrenderincontext:uigraphicsgetcurrentcontext()];

}Else{

[self. View . layerrenderincontext:uigraphicsgetcurrentcontext()];

}

UIImage * img = uigraphicsgetimagefromcurrentimagecontext();

uigraphicsendimagecontext();

return img;

}

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.