I want to set a UIImageView with a UIImage
and put this imageview inside a UIScrollView
to obtain a zoom of this image;
and I want this UIImageView
and UIScrollView
to fit in the rect at the center of the view...is it possible?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
I have written an example application which also supports AutoLayout and Storyboards to demonstrate this behavior. I hope it saves everyone time trying to figure this out: http://rexstjohn.com/facebook-like-ios-photo-modal-gallery-swipe-gestures/.
<UIScrollViewDelegate>
UIScrollView
the size you want for the rectangle at the center of the view. Set the max zoom in the inspector to something bigger than 1. Like 4 or 10.UIImageView
in theUIScrollView
and set it up with whatever image you want. Make it the same size as theUIScrollView
.UIImageView
to the.h
of your View controller to create anIBOutlet
for theUIImageView
, call it something clever likeimageView
.Add this code:
Run the app and pinch and pan til your heart's content.
With Swift 4 and iOS 11, you can use one of the two following solutions in order to solve your problem.
#1. Using insets
ViewController.swift
ImageScrollView.swift
#2. Using Auto Layout
ViewController.swift
ImageScrollView.swift
Sources:
Download this and this files. You'll need them to handle touches.
Add to your view the scrollView delegate
<UIScrollViewDelegate>
and declare the outlets:Import the downloaded file inside the screen and do:
Done!
Basically what this code do is to add the
imageView
as subview of theimageScrollView
.Then, it adds the
TapDetecting
class methods to the scrollView, in order to recognize the number of taps - the pinch the user do and add zoom functionalities.You can set the
minimumScale
of the image, if you leave1.0
the image should be displayed as-it-is (if you set it a little bit lower it's being scaled), and themaximumZoomScale
, i suggest you to leave it to 4, it's fine!Now, you can load images programmatically from there.
The last thing you have to do is to insert a
UIScrollView
inside your xib file and link it toimageScrollView
. You'll have the image at the perfect center, you can double tap on it to zoom, pinch to zoom as you set up in code.