Skip to content

Horizontal swipe improvements for more than one image #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BFRImageViewController/BFRImageContainerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
@property (nonatomic, assign) NSUInteger pageIndex;
/*! Assigning YES to this property will make the background transparent. */
@property (nonatomic, getter=isUsingTransparentBackground) BOOL useTransparentBackground;
/*! 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. */
@property (nonatomic, getter=shouldDisableHorizontalDrag) BOOL disableHorizontalDrag;
@end
12 changes: 11 additions & 1 deletion BFRImageViewController/BFRImageContainerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import <DACircularProgress/DACircularProgressView.h>
#import "AFNetworking.h"

@interface BFRImageContainerViewController () <UIScrollViewDelegate>
@interface BFRImageContainerViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UIImage *imgLoaded;
Expand Down Expand Up @@ -135,6 +135,9 @@ - (UIImageView *)createImageView {

//Dragging to dismiss
UIPanGestureRecognizer *panImg = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
if (self.shouldDisableHorizontalDrag) {
panImg.delegate = self;
}
[resizableImageView addGestureRecognizer:panImg];

return resizableImageView;
Expand All @@ -148,6 +151,13 @@ - (void)addImageToScrollView {
}
}

#pragma mark - Gesture Recognizer Delegate
//If we have more than one image, this will cancel out dragging horizontally to make it easy to navigate between images
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self.scrollView];
return fabs(velocity.y) > fabs(velocity.x);
}

#pragma mark - Scrollview Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.scrollView.subviews.firstObject;
Expand Down
26 changes: 1 addition & 25 deletions BFRImageViewController/BFRImageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (void)viewDidLoad {
BFRImageContainerViewController *imgVC = [BFRImageContainerViewController new];
imgVC.imgSrc = imgSrc;
imgVC.useTransparentBackground = self.isUsingTransparentBackground;
imgVC.disableHorizontalDrag = (self.images.count > 1);
[self.imageViewControllers addObject:imgVC];
}

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

#pragma mark - UI Methods
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];

//Buttons
[self.doneButton setFrame:CGRectMake(self.view.bounds.size.width - 75, 30, 55, 26)];
[self.view bringSubviewToFront:self.doneButton];
}

- (UIButton *)createDoneButton {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect screenBound = self.view.bounds;
CGFloat screenWidth = screenBound.size.width;
[btn setFrame:CGRectMake(screenWidth - 75, 30, 55, 26)];
[btn setTitleColor:[UIColor colorWithWhite:0.9 alpha:0.9] forState:UIControlStateNormal|UIControlStateHighlighted];
[btn setTitle:@"Done" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:11.0f]];
[btn setBackgroundColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
btn.layer.cornerRadius = 3.0f;
btn.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:0.9].CGColor;
btn.layer.borderWidth = 1.0f;

return btn;
}

#pragma mark - Pager Datasource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((BFRImageContainerViewController *)viewController).pageIndex;
Expand Down
23 changes: 0 additions & 23 deletions BFRImageViewerDemo/BFRImageViewer/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,8 @@
<view key="view" contentMode="scaleToFill" id="QS5-Rx-YEW">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="Second View" textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="zEq-FU-wV5">
<rect key="frame" x="195" y="279" width="209.5" height="41.5"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="36"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<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">
<rect key="frame" x="192" y="329" width="215.5" height="17"/>
<animations/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="NDk-cv-Gan" firstAttribute="top" secondItem="zEq-FU-wV5" secondAttribute="bottom" constant="8" symbolic="YES" id="Day-4N-Vmt"/>
<constraint firstItem="NDk-cv-Gan" firstAttribute="centerX" secondItem="zEq-FU-wV5" secondAttribute="centerX" id="JgO-Fn-dHn"/>
<constraint firstAttribute="centerX" secondItem="zEq-FU-wV5" secondAttribute="centerX" id="qqM-NS-xev"/>
<constraint firstAttribute="centerY" secondItem="zEq-FU-wV5" secondAttribute="centerY" id="qzY-Ky-pLD"/>
</constraints>
</view>
<tabBarItem key="tabBarItem" title="Second" image="second" id="cPa-gy-q4n"/>
</viewController>
Expand Down
2 changes: 1 addition & 1 deletion BFRImageViewerDemo/BFRImageViewer/FirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (void)viewDidLoad {
[self check3DTouch];
}

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

- (void)didReceiveMemoryWarning {
Expand Down
45 changes: 42 additions & 3 deletions BFRImageViewerDemo/BFRImageViewer/SecondViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,60 @@
//

#import "SecondViewController.h"
#import "BFRImageViewController.h"

@interface SecondViewController ()

@interface SecondViewController () <UIViewControllerPreviewingDelegate>
@property (strong, nonatomic) NSArray *imgURLs;
@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *openImageFromURL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
openImageFromURL.translatesAutoresizingMaskIntoConstraints = NO;
[openImageFromURL setTitle:@"Open Images" forState:UIControlStateNormal];
[openImageFromURL addTarget:self action:@selector(openImage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:openImageFromURL];
[openImageFromURL.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[openImageFromURL.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;

if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self check3DTouch];
}

NSURL *url1 = [NSURL URLWithString:@"https://bufferblog-wpengine.netdna-ssl.com/wp-content/uploads/2015/10/social-media-icons-800x565.jpg"];
NSURL *url2 = [NSURL URLWithString:@"https://open.buffer.com/wp-content/uploads/2015/11/new-journey-page.png"];
NSURL *url3 = [NSURL URLWithString:@"https://bufferblog-wpengine.netdna-ssl.com/wp-content/uploads/2015/10/how-to-create-social-media-sharing-schedule-800x400.png"];
self.imgURLs = @[url1, url2, url3];
}

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

- (void)openImage {
BFRImageViewController *imageVC = [[BFRImageViewController alloc] initWithImageSource:self.imgURLs];
[self presentViewController:imageVC animated:YES completion:nil];
}

#pragma mark - 3D Touch
- (void)check3DTouch {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
return [[BFRImageViewController alloc] initWithImageSource:self.imgURLs];
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self check3DTouch];
}
}
@end