Commit 4b0c4cf7 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Adjust SideSwipe animations

This CL updates the SideSwipe animations, using transforms instead of
modifying frames. Modifying frames wasn't working as expected as the
cards are positioned using autolayout.

Bug: 867968
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I3532a50a50078c07ee68e4c47f59c426c7dd461c
Reviewed-on: https://chromium-review.googlesource.com/1158824Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581174}
parent 16557148
......@@ -257,29 +257,22 @@ const CGFloat kResizeFactor = 4;
self.backgroundTopConstraint.constant = -StatusBarHeight();
}
- (CGRect)cardFrame {
return self.bounds;
- (CGFloat)cardWidth {
return CGRectGetWidth(self.bounds);
}
// Set up left and right card views depending on current tab and swipe
// direction.
- (void)updateViewsForDirection:(UISwipeGestureRecognizerDirection)direction {
_direction = direction;
CGRect cardFrame = [self cardFrame];
NSUInteger currentIndex = [model_ indexOfTab:model_.currentTab];
CGFloat offset = UseRTLLayout() ? -1 : 1;
if (_direction == UISwipeGestureRecognizerDirectionRight) {
[self setupCard:_rightCard withIndex:currentIndex];
[_rightCard setFrame:cardFrame];
[self setupCard:_leftCard withIndex:currentIndex - offset];
cardFrame.origin.x -= cardFrame.size.width + kCardHorizontalSpacing;
[_leftCard setFrame:cardFrame];
} else {
[self setupCard:_leftCard withIndex:currentIndex];
[_leftCard setFrame:cardFrame];
[self setupCard:_rightCard withIndex:currentIndex + offset];
cardFrame.origin.x += cardFrame.size.width + kCardHorizontalSpacing;
[_rightCard setFrame:cardFrame];
}
}
......@@ -336,40 +329,26 @@ const CGFloat kResizeFactor = 4;
});
}
// Place cards around |currentPoint_.x|, and lean towards each card near the
// X edges of |bounds|. Shrink cards as they are dragged towards the middle of
// the |bounds|, and edge cards only drag |kEdgeCardDragPercentage| of |bounds|.
// Move cards according to |currentPoint_.x|. Edge cards only drag
// |kEdgeCardDragPercentage| of |bounds|.
- (void)updateCardPositions {
CGRect bounds = [self cardFrame];
[_rightCard setFrame:bounds];
[_leftCard setFrame:bounds];
CGFloat width = [self cardWidth];
CGFloat width = CGRectGetWidth([self cardFrame]);
CGPoint center = CGPointMake(bounds.origin.x + bounds.size.width / 2,
bounds.origin.y + bounds.size.height / 2);
if ([self isEdgeSwipe]) {
// If an edge card, don't allow the card to be dragged across the screen.
// Instead, drag across |kEdgeCardDragPercentage| of the screen.
center.x = currentPoint_.x - width / 2 -
(currentPoint_.x - width) / width *
(width * (1 - kEdgeCardDragPercentage));
[_leftCard setCenter:center];
center.x = currentPoint_.x / width * (width * kEdgeCardDragPercentage) +
bounds.size.width / 2;
[_rightCard setCenter:center];
_rightCard.transform = CGAffineTransformMakeTranslation(
(currentPoint_.x) * kEdgeCardDragPercentage, 0);
_leftCard.transform = CGAffineTransformMakeTranslation(
(currentPoint_.x - width) * kEdgeCardDragPercentage, 0);
} else {
// Place cards around the finger as it is dragged across the screen.
// Place the finger between the cards in the middle of the screen, on the
// right card border when on the left side of the screen, and on the left
// card border when on the right side of the screen.
CGFloat rightXBuffer = kCardHorizontalSpacing * currentPoint_.x / width;
CGFloat leftXBuffer = kCardHorizontalSpacing - rightXBuffer;
center.x = currentPoint_.x - leftXBuffer - width / 2;
[_leftCard setCenter:center];
center.x = currentPoint_.x + rightXBuffer + width / 2;
[_rightCard setCenter:center];
_rightCard.transform =
CGAffineTransformMakeTranslation(currentPoint_.x + rightXBuffer, 0);
_leftCard.transform = CGAffineTransformMakeTranslation(
-width + currentPoint_.x - leftXBuffer, 0);
}
}
......@@ -380,7 +359,7 @@ const CGFloat kResizeFactor = 4;
// Since it's difficult to touch the very edge of the screen (touches tend to
// sit near x ~ 4), push the touch towards the edge.
CGFloat width = CGRectGetWidth([self cardFrame]);
CGFloat width = [self cardWidth];
CGFloat half = floor(width / 2);
CGFloat padding = floor(std::abs(currentPoint_.x - half) / half);
......@@ -420,38 +399,41 @@ const CGFloat kResizeFactor = 4;
if (currentIndex == NSNotFound)
return [_delegate sideSwipeViewDismissAnimationDidEnd:self];
CGRect finalSize = [self cardFrame];
CGFloat width = CGRectGetWidth([self cardFrame]);
CGRect leftFrame, rightFrame;
CGFloat width = [self cardWidth];
CGAffineTransform rightTransform, leftTransform;
SwipeView* dominantCard;
Tab* destinationTab = model_.currentTab;
CGFloat offset = UseRTLLayout() ? -1 : 1;
if (_direction == UISwipeGestureRecognizerDirectionRight) {
// If swipe is right and |currentPoint_.x| is over the first 1/3, move left.
if (currentPoint_.x > width / 3.0 && ![self isEdgeSwipe]) {
rightTransform =
CGAffineTransformMakeTranslation(width + kCardHorizontalSpacing, 0);
leftTransform = CGAffineTransformIdentity;
destinationTab = [model_ tabAtIndex:currentIndex - offset];
dominantCard = _leftCard;
rightFrame = leftFrame = finalSize;
rightFrame.origin.x += rightFrame.size.width + kCardHorizontalSpacing;
base::RecordAction(UserMetricsAction("MobileStackSwipeCompleted"));
} else {
leftTransform =
CGAffineTransformMakeTranslation(-width - kCardHorizontalSpacing, 0);
rightTransform = CGAffineTransformIdentity;
dominantCard = _rightCard;
leftFrame = rightFrame = finalSize;
leftFrame.origin.x -= rightFrame.size.width + kCardHorizontalSpacing;
base::RecordAction(UserMetricsAction("MobileStackSwipeCancelled"));
}
} else {
// If swipe is left and |currentPoint_.x| is over the first 1/3, move right.
if (currentPoint_.x < (width / 3.0) * 2.0 && ![self isEdgeSwipe]) {
leftTransform =
CGAffineTransformMakeTranslation(-width - kCardHorizontalSpacing, 0);
rightTransform = CGAffineTransformIdentity;
destinationTab = [model_ tabAtIndex:currentIndex + offset];
dominantCard = _rightCard;
leftFrame = rightFrame = finalSize;
leftFrame.origin.x -= rightFrame.size.width + kCardHorizontalSpacing;
base::RecordAction(UserMetricsAction("MobileStackSwipeCompleted"));
} else {
rightTransform =
CGAffineTransformMakeTranslation(width + kCardHorizontalSpacing, 0);
leftTransform = CGAffineTransformIdentity;
dominantCard = _leftCard;
rightFrame = leftFrame = finalSize;
rightFrame.origin.x += rightFrame.size.width + kCardHorizontalSpacing;
base::RecordAction(UserMetricsAction("MobileStackSwipeCancelled"));
}
}
......@@ -467,10 +449,8 @@ const CGFloat kResizeFactor = 4;
[UIView animateWithDuration:kAnimationDuration
animations:^{
[_leftCard setTransform:CGAffineTransformIdentity];
[_rightCard setTransform:CGAffineTransformIdentity];
[_leftCard setFrame:leftFrame];
[_rightCard setFrame:rightFrame];
_leftCard.transform = leftTransform;
_rightCard.transform = rightTransform;
}
completion:^(BOOL finished) {
[_leftCard setImage:nil];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment