Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added percentage based dragOffset properties. refs #10

- improves rotation behavior
- designated as the default means of setting drag offsets (the absolute properties override percentage based properties, when set)
  • Loading branch information...
commit 8190926ec1f025fdaa0575a120df78660434ff8c 1 parent 74daa84
@matej authored
View
22 MBPullDownController/MBPullDownController.h
@@ -64,17 +64,35 @@
/**
* The minimum drag distance required to trigger the open action.
- * Defaults to 100.f.
+ * Overrides openDragOffsetPercentage when set.
+ * Disabled when det to NAN (default).
*/
@property (nonatomic, assign) CGFloat openDragOffset;
/**
* The minimum drag distance required to trigged the close action.
- * Defaults to 44.f.
+ * Overrides closeDragOffsetPercentage when set.
+ * Disabled when det to NAN (default).
*/
@property (nonatomic, assign) CGFloat closeDragOffset;
/**
+ * The minimum drag distance required to trigger the open action,
+ * expressed as a percentage of the total view height.
+ * Valid values from 0.f to 1.f, disabled otherwise.
+ * Defaults to .2f.
+ */
+@property (nonatomic, assign) CGFloat openDragOffsetPercentage;
+
+/**
+ * The minimum drag distance required to trigged the close action,
+ * expressed as a percentage of the total view height.
+ * Valid values from 0.f to 1.f, disabled otherwise.
+ * Defaults to .05f.
+ */
+@property (nonatomic, assign) CGFloat closeDragOffsetPercentage;
+
+/**
* The current state (observable).
* Defaults to NO.
* @see setOpen:animated:
View
41 MBPullDownController/MBPullDownController.m
@@ -11,12 +11,15 @@
#import <UIKit/UIGestureRecognizerSubclass.h>
#import <objc/runtime.h>
#import <objc/message.h>
+#include <math.h>
static CGFloat const kDefaultClosedTopOffset = 44.f;
static CGFloat const kDefaultOpenBottomOffset = 44.f;
-static CGFloat const kDefaultOpenDragOffset = 100.f;
-static CGFloat const kDefaultCloseDragOffset = 44.f;
+static CGFloat const kDefaultOpenDragOffset = NAN;
+static CGFloat const kDefaultCloseDragOffset = NAN;
+static CGFloat const kDefaultOpenDragOffsetPercentage = .2;
+static CGFloat const kDefaultCloseDragOffsetPercentage = .05;
static NSInteger const kContainerViewTag = -1000001;
@@ -68,6 +71,8 @@ - (void)sharedInitialization {
_openBottomOffset = kDefaultOpenBottomOffset;
_openDragOffset = kDefaultOpenDragOffset;
_closeDragOffset = kDefaultCloseDragOffset;
+ _openDragOffsetPercentage = kDefaultOpenDragOffsetPercentage;
+ _closeDragOffsetPercentage = kDefaultCloseDragOffsetPercentage;
_backgroundView = [MBPullDownControllerBackgroundView new];
[[self class] updateInstanceCount:1];
}
@@ -113,7 +118,7 @@ - (void)setBackController:(UIViewController *)backController {
}
}
-#pragma mark - Open / close offsets
+#pragma mark - Offsets
- (void)setClosedTopOffset:(CGFloat)closedTopOffset {
[self setClosedTopOffset:closedTopOffset animated:NO];
@@ -141,6 +146,32 @@ - (void)setOpenBottomOffset:(CGFloat)openBottomOffset animated:(BOOL)animated {
}
}
+- (void)setOpenDragOffsetPercentage:(CGFloat)openDragOffsetPercentage {
+ NSAssert(openDragOffsetPercentage >= 0.f && openDragOffsetPercentage <= 1.f,
+ @"openDragOffsetPercentage out of bounds [0.f, 1.f]");
+ _openDragOffsetPercentage = openDragOffsetPercentage;
+}
+
+- (void)setCloseDragOffsetPercentage:(CGFloat)closeDragOffsetPercentage {
+ NSAssert(closeDragOffsetPercentage >= 0.f && closeDragOffsetPercentage <= 1.f,
+ @"closeDragOffsetPercentage out of bounds [0.f, 1.f]");
+ _closeDragOffsetPercentage = closeDragOffsetPercentage;
+}
+
+- (CGFloat)computedOpenDragOffset {
+ if (!isnan(_openDragOffset)) {
+ return _openDragOffset;
+ }
+ return _openDragOffsetPercentage * self.view.bounds.size.height;
+}
+
+- (CGFloat)computedCloseDragOffset {
+ if (!isnan(_closeDragOffset)) {
+ return _closeDragOffset;
+ }
+ return _closeDragOffsetPercentage * self.view.bounds.size.height;
+}
+
#pragma mark - Open / close actions
- (void)toggleOpenAnimated:(BOOL)animated {
@@ -289,10 +320,10 @@ - (void)checkOpenCloseConstraints {
BOOL open = self.open;
BOOL enabled = self.pullToToggleEnabled;
CGPoint offset = [self scrollView].contentOffset;
- if (!open && enabled && offset.y < - self.openDragOffset - self.closedTopOffset) {
+ if (!open && enabled && offset.y < - [self computedOpenDragOffset] - self.closedTopOffset) {
[self setOpen:YES animated:YES];
} else if (open) {
- if (enabled && offset.y > self.closeDragOffset - self.view.bounds.size.height + self.openBottomOffset) {
+ if (enabled && offset.y > [self computedCloseDragOffset] - self.view.bounds.size.height + self.openBottomOffset) {
[self setOpen:NO animated:YES];
} else {
[self setOpen:YES animated:YES];
Please sign in to comment.
Something went wrong with that request. Please try again.