From 2507f2a19146a0fb701817f52ed4e31bb7bb1f35 Mon Sep 17 00:00:00 2001 From: Leon Gersing Date: Wed, 29 Feb 2012 20:56:47 -0500 Subject: [PATCH 1/2] Improved performance for drag behavior by delaying calls to cache the topview. --- .../ECSlidingViewController.h | 5 ++- .../ECSlidingViewController.m | 39 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.h b/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.h index 7f9db16..aac63aa 100644 --- a/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.h +++ b/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.h @@ -28,7 +28,10 @@ typedef enum { } ECResetStrategy; /** ECSlidingViewController is a view controller container that presents its child view controllers in two layers. The top layer can be panned to reveal the layers below it. */ -@interface ECSlidingViewController : UIViewController +@interface ECSlidingViewController : UIViewController{ + CGPoint startTouchPosition; + BOOL topViewHasFocus; +} /** Returns the view controller that will be visible when the top view is slide to the right. diff --git a/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.m b/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.m index e879e4d..1f4eded 100644 --- a/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.m +++ b/ECSlidingViewController/Vendor/ECSlidingViewController/ECSlidingViewController.m @@ -6,11 +6,12 @@ // Copyright (c) 2012 EdgeCase. All rights reserved. // +#define HORIZ_SWIPE_DRAG_MIN 5 #import "ECSlidingViewController.h" @interface ECSlidingViewController() -@property (nonatomic, strong) UIButton *topViewSnapshot; +@property (nonatomic, strong) UIView *topViewSnapshot; @property (nonatomic, unsafe_unretained) CGFloat initialTouchPositionX; @property (nonatomic, unsafe_unretained) CGFloat initialHoizontalCenter; @property (nonatomic, strong) UIPanGestureRecognizer *panGesture; @@ -138,6 +139,9 @@ - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { + if(!topViewHasFocus){ + [self removeTopViewSnapshot]; + } if ([self underRightShowing] && ![self topViewIsOffScreen]) { [self updateTopViewHorizontalCenter:self.anchorLeftTopViewCenter]; } else if ([self underRightShowing] && [self topViewIsOffScreen]) { @@ -149,6 +153,12 @@ } } +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ + if(!topViewHasFocus){ + [self addTopViewSnapshot]; + } +} + - (void)updateTopViewHorizontalCenterWithRecognizer:(UIPanGestureRecognizer *)recognizer { CGPoint currentTouchPoint = [recognizer locationInView:self.view]; @@ -210,10 +220,11 @@ } else { self.panGesture.enabled = NO; } - if (complete) { complete(); } + topViewHasFocus = NO; + [self addTopViewSnapshot]; }]; } @@ -238,6 +249,8 @@ if (complete) { complete(); } + topViewHasFocus = NO; + [self addTopViewSnapshot]; }]; } @@ -246,6 +259,7 @@ [UIView animateWithDuration:0.25f animations:^{ [self updateTopViewHorizontalCenter:self.resettedCenter]; } completion:^(BOOL finished) { + topViewHasFocus = YES; [self topViewHorizontalCenterDidChange:self.resettedCenter]; }]; } @@ -318,9 +332,10 @@ - (void)addTopViewSnapshot { if (!self.topViewSnapshot.superview && !self.shouldAllowUserInteractionsWhenAnchored) { - self.topViewSnapshot = [[UIButton alloc] initWithFrame:self.topView.bounds]; - [self.topViewSnapshot setImage:[UIImage imageWithUIView:self.topView] forState:(UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected)]; + self.topViewSnapshot = [[UIView alloc] initWithFrame:self.topView.bounds]; + topViewSnapshot.layer.contents = (id)[UIImage imageWithUIView:self.topView].CGImage; [self.topView addSubview:self.topViewSnapshot]; + [self.topViewSnapshot addGestureRecognizer:self.resetTapGesture]; } } @@ -328,6 +343,7 @@ { if (self.topViewSnapshot.superview) { [self.topViewSnapshot removeFromSuperview]; + topViewSnapshot = nil; } } @@ -380,10 +396,7 @@ - (void)underLeftWillAppear { - [self addTopViewSnapshot]; - if (resetStrategy & ECTapping) { - [self.topView addGestureRecognizer:self.resetTapGesture]; - } + topViewHasFocus = NO; self.underRightView.hidden = YES; [self.underLeftViewController viewWillAppear:NO]; self.underLeftView.hidden = NO; @@ -391,10 +404,7 @@ - (void)underRightWillAppear { - [self addTopViewSnapshot]; - if (resetStrategy & ECTapping) { - [self.topView addGestureRecognizer:self.resetTapGesture]; - } + topViewHasFocus = NO; self.underLeftView.hidden = YES; [self.underRightViewController viewWillAppear:NO]; self.underRightView.hidden = NO; @@ -402,9 +412,14 @@ - (void)topDidReset { + topViewHasFocus = YES; [self.topView removeGestureRecognizer:self.resetTapGesture]; [self removeTopViewSnapshot]; self.panGesture.enabled = YES; } +//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ +// return YES; +//} + @end From 08b004c9731e68b50472fa7d4e1cf3c34a62d59b Mon Sep 17 00:00:00 2001 From: Mike Enriquez + Leon Gersing Date: Wed, 29 Feb 2012 21:31:32 -0500 Subject: [PATCH 2/2] Improve quality of snapshot --- .../UIImage+ImageWithUIView.h | 1 - .../UIImage+ImageWithUIView.m | 26 +++++-------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.h b/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.h index d5c280b..20b453b 100644 --- a/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.h +++ b/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.h @@ -1,6 +1,5 @@ // // UIImage+ImageWithUIView.h -// Taken from http://stackoverflow.com/a/7233268 // #import diff --git a/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.m b/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.m index 8f3f979..c1eba4e 100644 --- a/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.m +++ b/ECSlidingViewController/Vendor/ECSlidingViewController/UIImage+ImageWithUIView.m @@ -1,6 +1,5 @@ // // UIImage+ImageWithUIView.m -// Taken from http://stackoverflow.com/a/7233268 // #import "UIImage+ImageWithUIView.h" @@ -8,30 +7,17 @@ @implementation UIImage (ImageWithUIView) #pragma mark - #pragma mark TakeScreenShot -static CGContextRef createBitmapContext(int pixelsWide, int pixelsHigh) -{ - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGBitmapInfo bitmapInfo = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); - CGContextRef bitmapContext = CGBitmapContextCreate(nil, pixelsWide, pixelsHigh, 8, 0, colorSpace, bitmapInfo); - CGColorSpaceRelease(colorSpace); - - return bitmapContext; -} + (UIImage *)imageWithUIView:(UIView *)view { CGSize screenShotSize = view.bounds.size; - CGContextRef contextRef = createBitmapContext(screenShotSize.width, screenShotSize.height); - CGContextTranslateCTM (contextRef, 0, screenShotSize.height); - CGContextScaleCTM(contextRef, 1, -1); + UIImage *img; + UIGraphicsBeginImageContext(screenShotSize); + CGContextRef ctx = UIGraphicsGetCurrentContext(); + [view drawLayer:view.layer inContext:ctx]; + img = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); - [view.layer renderInContext:contextRef]; - CGImageRef imageRef = CGBitmapContextCreateImage(contextRef); - CGContextRelease(contextRef); - - UIImage *img = [UIImage imageWithCGImage:imageRef]; - CGImageRelease(imageRef); - // return the image return img; } @end