Skip to content

Commit 29b38b3

Browse files
author
Jordan Morgan
committed
Merge pull request #2 from bufferapp/task/tweak-multiple-image-gestures
Horizontal swipe improvements for more than one image
2 parents e03ba00 + 772a1c7 commit 29b38b3

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed

Diff for: BFRImageViewController/BFRImageContainerViewController.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
@property (nonatomic, assign) NSUInteger pageIndex;
1717
/*! Assigning YES to this property will make the background transparent. */
1818
@property (nonatomic, getter=isUsingTransparentBackground) BOOL useTransparentBackground;
19+
/*! If there is more than one image in the containing @c BFRImageViewController - this property is set to YES to make swiping from image to image easier. */
20+
@property (nonatomic, getter=shouldDisableHorizontalDrag) BOOL disableHorizontalDrag;
1921
@end

Diff for: BFRImageViewController/BFRImageContainerViewController.m

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import <DACircularProgress/DACircularProgressView.h>
1212
#import "AFNetworking.h"
1313

14-
@interface BFRImageContainerViewController () <UIScrollViewDelegate>
14+
@interface BFRImageContainerViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate>
1515
@property (strong, nonatomic) UIScrollView *scrollView;
1616
@property (strong, nonatomic) UIImageView *imgView;
1717
@property (strong, nonatomic) UIImage *imgLoaded;
@@ -135,6 +135,9 @@ - (UIImageView *)createImageView {
135135

136136
//Dragging to dismiss
137137
UIPanGestureRecognizer *panImg = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
138+
if (self.shouldDisableHorizontalDrag) {
139+
panImg.delegate = self;
140+
}
138141
[resizableImageView addGestureRecognizer:panImg];
139142

140143
return resizableImageView;
@@ -148,6 +151,13 @@ - (void)addImageToScrollView {
148151
}
149152
}
150153

154+
#pragma mark - Gesture Recognizer Delegate
155+
//If we have more than one image, this will cancel out dragging horizontally to make it easy to navigate between images
156+
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
157+
CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self.scrollView];
158+
return fabs(velocity.y) > fabs(velocity.x);
159+
}
160+
151161
#pragma mark - Scrollview Delegate
152162
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
153163
return self.scrollView.subviews.firstObject;

Diff for: BFRImageViewController/BFRImageViewController.m

+1-25
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ - (void)viewDidLoad {
5353
BFRImageContainerViewController *imgVC = [BFRImageContainerViewController new];
5454
imgVC.imgSrc = imgSrc;
5555
imgVC.useTransparentBackground = self.isUsingTransparentBackground;
56+
imgVC.disableHorizontalDrag = (self.images.count > 1);
5657
[self.imageViewControllers addObject:imgVC];
5758
}
5859

@@ -76,31 +77,6 @@ - (void)dealloc {
7677
[[NSNotificationCenter defaultCenter] removeObserver:self];
7778
}
7879

79-
#pragma mark - UI Methods
80-
- (void)viewWillLayoutSubviews {
81-
[super viewWillLayoutSubviews];
82-
83-
//Buttons
84-
[self.doneButton setFrame:CGRectMake(self.view.bounds.size.width - 75, 30, 55, 26)];
85-
[self.view bringSubviewToFront:self.doneButton];
86-
}
87-
88-
- (UIButton *)createDoneButton {
89-
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
90-
CGRect screenBound = self.view.bounds;
91-
CGFloat screenWidth = screenBound.size.width;
92-
[btn setFrame:CGRectMake(screenWidth - 75, 30, 55, 26)];
93-
[btn setTitleColor:[UIColor colorWithWhite:0.9 alpha:0.9] forState:UIControlStateNormal|UIControlStateHighlighted];
94-
[btn setTitle:@"Done" forState:UIControlStateNormal];
95-
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:11.0f]];
96-
[btn setBackgroundColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
97-
btn.layer.cornerRadius = 3.0f;
98-
btn.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:0.9].CGColor;
99-
btn.layer.borderWidth = 1.0f;
100-
101-
return btn;
102-
}
103-
10480
#pragma mark - Pager Datasource
10581
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
10682
NSUInteger index = ((BFRImageContainerViewController *)viewController).pageIndex;

Diff for: BFRImageViewerDemo/BFRImageViewer/Base.lproj/Main.storyboard

-23
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,8 @@
3636
<view key="view" contentMode="scaleToFill" id="QS5-Rx-YEW">
3737
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
3838
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
39-
<subviews>
40-
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="Second View" textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="zEq-FU-wV5">
41-
<rect key="frame" x="195" y="279" width="209.5" height="41.5"/>
42-
<animations/>
43-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
44-
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="36"/>
45-
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
46-
<nil key="highlightedColor"/>
47-
</label>
48-
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Loaded by SecondViewController" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="NDk-cv-Gan">
49-
<rect key="frame" x="192" y="329" width="215.5" height="17"/>
50-
<animations/>
51-
<fontDescription key="fontDescription" type="system" pointSize="14"/>
52-
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
53-
<nil key="highlightedColor"/>
54-
</label>
55-
</subviews>
5639
<animations/>
5740
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
58-
<constraints>
59-
<constraint firstItem="NDk-cv-Gan" firstAttribute="top" secondItem="zEq-FU-wV5" secondAttribute="bottom" constant="8" symbolic="YES" id="Day-4N-Vmt"/>
60-
<constraint firstItem="NDk-cv-Gan" firstAttribute="centerX" secondItem="zEq-FU-wV5" secondAttribute="centerX" id="JgO-Fn-dHn"/>
61-
<constraint firstAttribute="centerX" secondItem="zEq-FU-wV5" secondAttribute="centerX" id="qqM-NS-xev"/>
62-
<constraint firstAttribute="centerY" secondItem="zEq-FU-wV5" secondAttribute="centerY" id="qzY-Ky-pLD"/>
63-
</constraints>
6441
</view>
6542
<tabBarItem key="tabBarItem" title="Second" image="second" id="cPa-gy-q4n"/>
6643
</viewController>

Diff for: BFRImageViewerDemo/BFRImageViewer/FirstViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ - (void)viewDidLoad {
3030
[self check3DTouch];
3131
}
3232

33-
self.imgURL = [NSURL URLWithString:@"https://pmcvariety.files.wordpress.com/2014/04/36820.jpg?w=670&h=377&crop=1"];
33+
self.imgURL = [NSURL URLWithString:@"https://bufferblog-wpengine.netdna-ssl.com/wp-content/uploads/2015/10/social-media-icons-800x565.jpg"];
3434
}
3535

3636
- (void)didReceiveMemoryWarning {

Diff for: BFRImageViewerDemo/BFRImageViewer/SecondViewController.m

+42-3
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,60 @@
77
//
88

99
#import "SecondViewController.h"
10+
#import "BFRImageViewController.h"
1011

11-
@interface SecondViewController ()
12-
12+
@interface SecondViewController () <UIViewControllerPreviewingDelegate>
13+
@property (strong, nonatomic) NSArray *imgURLs;
1314
@end
1415

1516
@implementation SecondViewController
1617

1718
- (void)viewDidLoad {
1819
[super viewDidLoad];
1920
// Do any additional setup after loading the view, typically from a nib.
21+
UIButton *openImageFromURL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
22+
openImageFromURL.translatesAutoresizingMaskIntoConstraints = NO;
23+
[openImageFromURL setTitle:@"Open Images" forState:UIControlStateNormal];
24+
[openImageFromURL addTarget:self action:@selector(openImage) forControlEvents:UIControlEventTouchUpInside];
25+
[self.view addSubview:openImageFromURL];
26+
[openImageFromURL.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
27+
[openImageFromURL.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
28+
29+
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
30+
[self check3DTouch];
31+
}
32+
33+
NSURL *url1 = [NSURL URLWithString:@"https://bufferblog-wpengine.netdna-ssl.com/wp-content/uploads/2015/10/social-media-icons-800x565.jpg"];
34+
NSURL *url2 = [NSURL URLWithString:@"https://open.buffer.com/wp-content/uploads/2015/11/new-journey-page.png"];
35+
NSURL *url3 = [NSURL URLWithString:@"https://bufferblog-wpengine.netdna-ssl.com/wp-content/uploads/2015/10/how-to-create-social-media-sharing-schedule-800x400.png"];
36+
self.imgURLs = @[url1, url2, url3];
2037
}
2138

2239
- (void)didReceiveMemoryWarning {
23-
[super didReceiveMemoryWarning];
2440
// Dispose of any resources that can be recreated.
2541
}
2642

43+
- (void)openImage {
44+
BFRImageViewController *imageVC = [[BFRImageViewController alloc] initWithImageSource:self.imgURLs];
45+
[self presentViewController:imageVC animated:YES completion:nil];
46+
}
47+
48+
#pragma mark - 3D Touch
49+
- (void)check3DTouch {
50+
[self registerForPreviewingWithDelegate:self sourceView:self.view];
51+
}
52+
53+
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
54+
return [[BFRImageViewController alloc] initWithImageSource:self.imgURLs];
55+
}
56+
57+
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
58+
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
59+
}
60+
61+
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
62+
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
63+
[self check3DTouch];
64+
}
65+
}
2766
@end

0 commit comments

Comments
 (0)