Skip to content
Open
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
1 change: 1 addition & 0 deletions PhotoView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default class PhotoView extends Component {
fadeDuration: PropTypes.number,
minimumZoomScale: PropTypes.number,
maximumZoomScale: PropTypes.number,
allowsInitialPan: PropTypes.bool,
scale: PropTypes.number,
onLoadStart: PropTypes.func,
onLoad: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Basics:
| fadeDuration | int | duration of image fade (in ms) |
| minimumZoomScale | float | The minimum allowed zoom scale. The default value is 1.0 |
| maximumZoomScale | float | The maximum allowed zoom scale. The default value is 3.0 |
| allowsInitialPan | bool | **iOS only**: When true, allows user to pan image before pinching first. The default value is false. |
| showsHorizontalScrollIndicator | bool | **iOS only**: When true, shows a horizontal scroll indicator. The default value is true. |
| showsVerticalScrollIndicator | bool | **iOS only**: When true, shows a vertical scroll indicator. The default value is true. |
| scale | float | Set zoom scale programmatically |
Expand Down
1 change: 1 addition & 0 deletions ios/RNPhotoView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@property (nonatomic, assign) NSInteger scale;
@property (nonatomic, assign) CGFloat minZoomScale;
@property (nonatomic, assign) CGFloat maxZoomScale;
@property (nonatomic, assign) BOOL allowsInitialPan;

#pragma mark - Block

Expand Down
67 changes: 34 additions & 33 deletions ios/RNPhotoView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
self.scrollEnabled = YES; // reset
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
Expand Down Expand Up @@ -87,7 +87,7 @@ - (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch {
touchY *= 1/self.zoomScale;
touchX += self.contentOffset.x;
touchY += self.contentOffset.y;

if (_onPhotoViewerTap) {
_onPhotoViewerTap(@{
@"point": @{
Expand All @@ -114,7 +114,7 @@ - (void)view:(UIView *)view singleTapDetected:(UITouch *)touch {
touchY *= 1/self.zoomScale;
touchX += self.contentOffset.x;
touchY += self.contentOffset.y;

if (_onPhotoViewerViewTap) {
_onPhotoViewerViewTap(@{
@"point": @{
Expand Down Expand Up @@ -160,27 +160,27 @@ - (CGFloat)initialZoomScaleWithMinScale {
}

- (void)setMaxMinZoomScalesForCurrentBounds {

// Reset
self.maximumZoomScale = 1;
self.minimumZoomScale = 1;
self.zoomScale = 1;

// Bail if no image
if (_photoImageView.image == nil) return;

// Reset position
_photoImageView.frame = CGRectMake(0, 0, _photoImageView.frame.size.width, _photoImageView.frame.size.height);

// Sizes
CGSize boundsSize = self.bounds.size;
CGSize imageSize = _photoImageView.image.size;

// Calculate Min
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible

/**
[attention]
original maximumZoomScale and minimumZoomScale is scaled to image,
Expand All @@ -189,59 +189,60 @@ - (void)setMaxMinZoomScalesForCurrentBounds {
*/
CGFloat maxScale = minScale * _maxZoomScale;
minScale = minScale * _minZoomScale;

// Set min/max zoom
self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;

// Initial zoom
self.zoomScale = [self initialZoomScaleWithMinScale];

// If we're zooming to fill then centralise
if (self.zoomScale != minScale) {

// Centralise
self.contentOffset = CGPointMake((imageSize.width * self.zoomScale - boundsSize.width) / 2.0,
(imageSize.height * self.zoomScale - boundsSize.height) / 2.0);

}

// Disable scrolling initially until the first pinch to fix issues with swiping on an initally zoomed in photo
self.scrollEnabled = NO;


// Unless explicitly enabled, disable scrolling initially until the first pinch.
// Fixes issues with swiping on an initally zoomed in photo.
self.scrollEnabled = _allowsInitialPan;

// Layout
[self setNeedsLayout];

}

#pragma mark - Layout

- (void)layoutSubviews {

// Update tap view frame
_tapView.frame = self.bounds;

// Super
[super layoutSubviews];

// Center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = _photoImageView.frame;

// Horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = floorf((boundsSize.width - frameToCenter.size.width) / 2.0);
} else {
frameToCenter.origin.x = 0;
}

// Vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = floorf((boundsSize.height - frameToCenter.size.height) / 2.0);
} else {
frameToCenter.origin.y = 0;
}

// Center
if (!CGRectEqualToRect(_photoImageView.frame, frameToCenter))
_photoImageView.frame = frameToCenter;
Expand All @@ -258,24 +259,24 @@ - (void)layoutSubviews {
// Get and display image
- (void)displayWithImage:(UIImage*)image {
if (image && !_photoImageView.image) {

// Reset
// self.maximumZoomScale = 1;
// self.minimumZoomScale = 1;
self.zoomScale = 1;
self.contentSize = CGSizeMake(0, 0);

// Set image
_photoImageView.image = image;
_photoImageView.hidden = NO;

// Setup photo frame
CGRect photoImageViewFrame;
photoImageViewFrame.origin = CGPointZero;
photoImageViewFrame.size = image.size;
_photoImageView.frame = photoImageViewFrame;
self.contentSize = photoImageViewFrame.size;

// Set zoom to minimum zoom
[self setMaxMinZoomScalesForCurrentBounds];
[self setNeedsLayout];
Expand Down Expand Up @@ -386,21 +387,21 @@ - (void)setScale:(NSInteger)scale {
- (void)initView {
_minZoomScale = 1.0;
_maxZoomScale = 5.0;

// Setup
self.backgroundColor = [UIColor whiteColor];
self.delegate = self;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.showsVerticalScrollIndicator = YES;
self.showsHorizontalScrollIndicator = YES;

// Tap view for background
_tapView = [[MWTapDetectingView alloc] initWithFrame:self.bounds];
_tapView.tapDelegate = self;
_tapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tapView.backgroundColor = [UIColor whiteColor];
[self addSubview:_tapView];

// Image view
_photoImageView = [[MWTapDetectingImageView alloc] initWithFrame:self.bounds];
_photoImageView.backgroundColor = [UIColor whiteColor];
Expand Down
1 change: 1 addition & 0 deletions ios/RNPhotoViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ - (UIView *)view {

RCT_EXPORT_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(allowsInitialPan, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onPhotoViewerError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPhotoViewerScale, RCTDirectEventBlock)
Expand Down