Skip to content

Commit

Permalink
fixes bugs and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
zhxnlai committed Jan 19, 2015
1 parent a11f88a commit 8ab97ee
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 81 deletions.
124 changes: 65 additions & 59 deletions ZLSwipeableView/ZLSwipeableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ - (void)setup {
self.programaticSwipeRotationRelativeYOffsetFromCenter = -0.2;
self.swipeableViewsCenter =
CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
self.swipeableViewsCenterInitial = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
self.swipeableViewsCenterInitial =
CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
self.collisionRect = [self defaultCollisionRect];
}

Expand All @@ -86,6 +87,11 @@ - (void)layoutSubviews {
self.anchorContainerView.frame = CGRectMake(0, 0, 1, 1);
self.containerView.frame = self.bounds;
self.reuseCoverContainerView.frame = self.bounds;
self.swipeableViewsCenterInitial = CGPointMake(
self.bounds.size.width / 2 + self.swipeableViewsCenterInitial.x -
self.swipeableViewsCenter.x,
self.bounds.size.height / 2 + self.swipeableViewsCenterInitial.y -
self.swipeableViewsCenter.y);
self.swipeableViewsCenter =
CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
}
Expand Down Expand Up @@ -242,20 +248,24 @@ - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
sqrt(pow(velocity.x, 2) + pow(velocity.y, 2));
CGPoint normalizedVelocity = CGPointMake(
velocity.x / velocityMagnitude, velocity.y / velocityMagnitude);
if (((signum(translation.x) == -1 && self.direction & ZLSwipeableViewDirectionLeft) ||
(signum(translation.x) == 1 && self.direction & ZLSwipeableViewDirectionRight) ||
(signum(translation.y) == -1 && self.direction & ZLSwipeableViewDirectionDown) ||
(signum(translation.y) == 1 && self.direction & ZLSwipeableViewDirectionUp)
) &&
if (((signum(translation.x) == -1 &&
self.direction & ZLSwipeableViewDirectionLeft) ||
(signum(translation.x) == 1 &&
self.direction & ZLSwipeableViewDirectionRight) ||
(signum(translation.y) == -1 &&
self.direction & ZLSwipeableViewDirectionDown) ||
(signum(translation.y) == 1 &&
self.direction & ZLSwipeableViewDirectionUp)) &&
(ABS(translation.x) > self.relativeDisplacementThreshold *
self.bounds.size.width // displacement
||
velocityMagnitude > self.escapeVelocityThreshold) // velocity
&&
(signum(translation.x) == signum(normalizedVelocity.x)) // sign X
&& (signum(translation.y) == signum(normalizedVelocity.y)) // sign Y
/*&& ABS(normalizedVelocity.y) < 0.8f*/) // confine vertical direction
{
/*&& ABS(normalizedVelocity.y) < 0.8f*/) // confine vertical
// direction
{
CGFloat scale = velocityMagnitude > self.escapeVelocityThreshold
? velocityMagnitude
: self.pushVelocityMagnitude;
Expand Down Expand Up @@ -305,15 +315,14 @@ - (void)swipeTopViewToRight {
}

- (void)swipeTopViewToUp {
[self swipeTopViewToUp:YES];
[self swipeTopViewToUp:YES];
}

- (void)swipeTopViewToDown {
[self swipeTopViewToUp:NO];
[self swipeTopViewToUp:NO];
}

- (void)swipeTopViewToLeft:(BOOL)left
{
- (void)swipeTopViewToLeft:(BOOL)left {
UIView *topSwipeableView = [self topSwipeableView];
if (!topSwipeableView) {
return;
Expand All @@ -333,25 +342,24 @@ - (void)swipeTopViewToLeft:(BOOL)left
andCollideInRect:self.collisionRect];
}

- (void)swipeTopViewToUp:(BOOL)up
{
UIView *topSwipeableView = [self topSwipeableView];
if (!topSwipeableView) {
return;
}

CGPoint location = CGPointMake(
topSwipeableView.center.x,
topSwipeableView.center.y *
(1 + self.programaticSwipeRotationRelativeYOffsetFromCenter));
[self createAnchorViewForCover:topSwipeableView
atLocation:location
shouldAttachAnchorViewToPoint:YES];
CGVector direction =
CGVectorMake(0,(up ? -1 : 1) * self.escapeVelocityThreshold);
[self pushAnchorViewForCover:topSwipeableView
inDirection:direction
andCollideInRect:self.collisionRect];
- (void)swipeTopViewToUp:(BOOL)up {
UIView *topSwipeableView = [self topSwipeableView];
if (!topSwipeableView) {
return;
}

CGPoint location = CGPointMake(
topSwipeableView.center.x,
topSwipeableView.center.y *
(1 + self.programaticSwipeRotationRelativeYOffsetFromCenter));
[self createAnchorViewForCover:topSwipeableView
atLocation:location
shouldAttachAnchorViewToPoint:YES];
CGVector direction =
CGVectorMake(0, (up ? -1 : 1) * self.escapeVelocityThreshold);
[self pushAnchorViewForCover:topSwipeableView
inDirection:direction
andCollideInRect:self.collisionRect];
}

#pragma mark - UIDynamicAnimationHelpers
Expand Down Expand Up @@ -454,34 +462,32 @@ - (void)createAnchorViewForCover:(UIView *)swipeableView

- (void)pushAnchorViewForCover:(UIView *)swipeableView
inDirection:(CGVector)direction
andCollideInRect:(CGRect)collisionRect
{
if(ABS(direction.dx) > ABS(direction.dy))
{
if (direction.dx > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didSwipeRight:)])
[self.delegate swipeableView:self didSwipeRight:swipeableView];
}
else
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didSwipeLeft:)])
[self.delegate swipeableView:self didSwipeLeft:swipeableView];
}
}
else
{
if (direction.dy > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didSwipeDown:)])
[self.delegate swipeableView:self didSwipeDown:swipeableView];
}
else
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableView:didSwipeUp:)])
[self.delegate swipeableView:self didSwipeUp:swipeableView];
}
}
andCollideInRect:(CGRect)collisionRect {
if (ABS(direction.dx) > ABS(direction.dy)) {
if (direction.dx > 0) {
if (self.delegate &&
[self.delegate
respondsToSelector:@selector(swipeableView:didSwipeRight:)])
[self.delegate swipeableView:self didSwipeRight:swipeableView];
} else {
if (self.delegate &&
[self.delegate
respondsToSelector:@selector(swipeableView:didSwipeLeft:)])
[self.delegate swipeableView:self didSwipeLeft:swipeableView];
}
} else {
if (direction.dy > 0) {
if (self.delegate &&
[self.delegate
respondsToSelector:@selector(swipeableView:didSwipeDown:)])
[self.delegate swipeableView:self didSwipeDown:swipeableView];
} else {
if (self.delegate &&
[self.delegate
respondsToSelector:@selector(swipeableView:didSwipeUp:)])
[self.delegate swipeableView:self didSwipeUp:swipeableView];
}
}
[self.animator removeBehavior:self.anchorViewAttachmentBehavior];

UICollisionBehavior *collisionBehavior =
Expand Down
37 changes: 15 additions & 22 deletions ZLSwipeableViewDemo/ZLSwipeableViewDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ - (void)viewDidLoad {

// Optional Delegate
self.swipeableView.delegate = self;

self.swipeableView.swipeableViewsCenterInitial = CGPointMake(-200, -200);
}

- (void)viewDidLayoutSubviews {
Expand All @@ -71,13 +69,11 @@ - (IBAction)swipeLeftButtonAction:(UIButton *)sender {
- (IBAction)swipeRightButtonAction:(UIButton *)sender {
[self.swipeableView swipeTopViewToRight];
}
- (IBAction)swipeUpButtonAction:(UIButton *)sender
{
[self.swipeableView swipeTopViewToUp];
- (IBAction)swipeUpButtonAction:(UIButton *)sender {
[self.swipeableView swipeTopViewToUp];
}
- (IBAction)swipeDownButtonAction:(UIButton *)sender
{
[self.swipeableView swipeTopViewToDown];
- (IBAction)swipeDownButtonAction:(UIButton *)sender {
[self.swipeableView swipeTopViewToDown];
}

- (IBAction)reloadButtonAction:(UIButton *)sender {
Expand All @@ -104,15 +100,13 @@ - (void)actionSheet:(UIActionSheet *)actionSheet
#pragma mark - ZLSwipeableViewDelegate

- (void)swipeableView:(ZLSwipeableView *)swipeableView
didSwipeUp:(UIView *)view
{
NSLog(@"did swipe up");
didSwipeUp:(UIView *)view {
NSLog(@"did swipe up");
}

- (void)swipeableView:(ZLSwipeableView *)swipeableView
didSwipeDown:(UIView *)view
{
NSLog(@"did swipe down");
didSwipeDown:(UIView *)view {
NSLog(@"did swipe down");
}

- (void)swipeableView:(ZLSwipeableView *)swipeableView
Expand Down Expand Up @@ -152,8 +146,7 @@ - (void)swipeableView:(ZLSwipeableView *)swipeableView

#pragma mark - ZLSwipeableViewDataSource

- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView
{
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
if (self.colorIndex < self.colors.count) {
CardView *view = [[CardView alloc] initWithFrame:swipeableView.bounds];
view.backgroundColor = [self colorForName:self.colors[self.colorIndex]];
Expand All @@ -174,12 +167,12 @@ - (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView
@"width" : @(view.bounds.size.width)
};
NSDictionary *views = NSDictionaryOfVariableBindings(contentView);
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:
@"H:|[contentView(width)]"
options:0
metrics:metrics
views:views]];
[view addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[contentView(width)]"
options:0
metrics:metrics
views:views]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:
@"V:|[contentView(height)]"
Expand Down

0 comments on commit 8ab97ee

Please sign in to comment.