我目前正在尝试了解如何将两个PFImageView
图片导入我的scrollView
。
scrollView
将能够平移和缩放两个图像。
图像将相互叠加,当您按下长按手势时,顶部图像会消失,显示下方的图像。
我已经能够显示图像,平移,缩放,我有手势可以工作。现在唯一不起作用的是约束。
单独我的代码适用于每个图像,但我不确定如何将它们都实现到函数(viewForZoomingInScrollView
)中。
当我在其中放置一个图像(return image1
)时,图像加载完美,所有约束都起作用,而另一个图像很大!当我放入(return image2
)时,第一个图像扭曲,第二个完美。
我尝试过子查看但没有成功。
有人可以查看我的代码并告诉我哪里出错了吗?
import Parse
import ParseUI
import UIKit
import SwiftSpinner
let imageScrollLargeImageName3 = PFImageView()
let imageScrollSmallImageName3 = PFImageView()
class ExplanationZoom: UIViewController, UIScrollViewDelegate {
var currentObject : PFObject?
@IBOutlet weak var image: PFImageView!
@IBOutlet weak var image2: PFImageView!
@IBOutlet weak var imageConstraintTop3: NSLayoutConstraint!
@IBOutlet weak var imageConstraintRight3: NSLayoutConstraint!
@IBOutlet weak var imageConstraintLeft3: NSLayoutConstraint!
@IBOutlet weak var imageConstraintBottom3: NSLayoutConstraint!
@IBOutlet weak var imageConstraintLeft4: NSLayoutConstraint!
@IBOutlet weak var imageConstraintTop4: NSLayoutConstraint!
@IBOutlet weak var imageConstraintRight4: NSLayoutConstraint!
@IBOutlet weak var imageConstraintBottom4: NSLayoutConstraint!
@IBAction func doneButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func moreInfovarcelltableViewdequeueReusableCellWithIdentifierCustomCellasCustomCellifcellnilcellCustomCellstyleUITableViewCellStyleDefaultreuseIdentifierCustomCellExtractvaluesfromthePFObjecttodisplayinthetablecellifletnameConditionobjectnameConditionasStringcellcustomNameConditiontextnameConditionDisplayflagimagevarinitialThumbnailUIImagenamedquestioncellcustomImageimageinitialThumbnailifletthumbnailobjectimageasPFFilecellcustomImagefilethumbnailcellcustomImageloadInBackgroundreturncell(sender: UIButton) {
}
@IBOutlet weak var imageSizeToggleButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
var lastZoomScale: CGFloat = -1
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
scrollView.delegate = self
var gesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
gesture.minimumPressDuration = 1.0
self.view.addGestureRecognizer(gesture)
updateZoom()
updateZoom2()
SwiftSpinner.show("Loading Image...")
// Unwrap the current object object
if let object = currentObject {
var initialThumbnail = UIImage(named: "question")
image.image = initialThumbnail
if let thumbnail = object["Image"] as? PFFile {
image.file = thumbnail
image.loadInBackground()
var imageView = PFImageView(frame: CGRectMake(self.scrollView.frame.width, 0, self.scrollView.frame.width, 50))
imageView.backgroundColor = UIColor.blackColor()
imageView.clipsToBounds = true
imageView.userInteractionEnabled = true
imageView.contentMode = .Center
imageView.contentMode = .ScaleAspectFill
imageView.autoresizingMask = (.FlexibleBottomMargin | .FlexibleHeight | .FlexibleLeftMargin | .FlexibleRightMargin | .FlexibleTopMargin | .FlexibleWidth)
scrollView.addSubview(image)
image.loadInBackground()
var imgView = UIImage (named: "image")
updateConstraints()
updateViewConstraints()
}
if let thumbnail2 = object["pressImage"] as? PFFile {
image2.file = thumbnail2
image2.loadInBackground()
var imgView = UIImage (named: "image2")
var imageView = PFImageView(frame: CGRectMake(self.scrollView.frame.width, 0, self.scrollView.frame.width, 50))
imageView.backgroundColor = UIColor.blackColor()
imageView.clipsToBounds = true
imageView.userInteractionEnabled = true
imageView.contentMode = .Center
imageView.contentMode = .ScaleAspectFill
imageView.autoresizingMask = (.FlexibleBottomMargin | .FlexibleHeight | .FlexibleLeftMargin | .FlexibleRightMargin | .FlexibleTopMargin | .FlexibleWidth)
scrollView.addSubview(image2)
image2.loadInBackground()
//updateConstraints2()
updateViewConstraints()
//image2.hidden = true
}
updateZoom()
updateZoom2()
}
updateZoom()
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func longPressed(longPress: UIGestureRecognizer){
if (longPress.state == UIGestureRecognizerState.Ended) {
println("Ended")
image.hidden = false
image2.hidden = true
}
else if (longPress.state == UIGestureRecognizerState.Began){
println("Began")
image.hidden = true
image2.hidden = false
}
}
func update() {
SwiftSpinner.hide()
}
override func willAnimateRotationToInterfaceOrientation(
toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
super.willAnimateRotationToInterfaceOrientation(toInterfaceOrientation, duration: duration)
updateZoom()
updateZoom2()
}
func updateConstraints() {
if let image = image.image {
let imageWidth = image.size.width
let imageHeight = image.size.height
let viewWidth = view.bounds.size.width
let viewHeight = view.bounds.size.height
NSLog ("\(scrollView)")
// center image if it is smaller than screen
var hPadding = (viewWidth - scrollView.zoomScale * imageWidth) / 2
if hPadding < 0 { hPadding = 0 }
var vPadding = (viewHeight - scrollView.zoomScale * imageHeight) / 2
if vPadding < 0 { vPadding = 0 }
NSLog ("\(imageConstraintBottom3)")
imageConstraintLeft3.constant = hPadding
imageConstraintRight3.constant = hPadding
imageConstraintTop3.constant = vPadding
imageConstraintBottom3.constant = vPadding
}
if let image2 = image2.image {
let imageWidth2 = image2.size.width
let imageHeight2 = image2.size.height
let viewWidth = view.bounds.size.width
let viewHeight = view.bounds.size.height
var hPadding2 = (viewWidth - scrollView.zoomScale * imageWidth2) / 2
if hPadding2 < 0 { hPadding2 = 0 }
var vPadding2 = (viewHeight - scrollView.zoomScale * imageHeight2) / 2
if vPadding2 < 0 { vPadding2 = 0 }
NSLog ("\(imageConstraintBottom3)")
imageConstraintLeft4.constant = hPadding2
imageConstraintRight4.constant = hPadding2
imageConstraintTop4.constant = vPadding2
imageConstraintBottom4.constant = vPadding2
// Makes zoom out animation smooth and starting from the right point not from (0, 0)
view.layoutIfNeeded()
}
}
// Zoom to show as much image as possible unless image is smaller than screen
private func updateZoom() {
if let image = image.image {
var minZoom = min(view.bounds.size.width / image.size.width,
view.bounds.size.height / image.size.height)
NSLog ("\(minZoom)")
if minZoom > 1 { minZoom = 1 }
scrollView.minimumZoomScale = minZoom
// Force scrollViewDidZoom fire if zoom did not change
if minZoom == lastZoomScale { minZoom += 0.000001 }
scrollView.zoomScale = minZoom
lastZoomScale = minZoom
}
}
private func updateZoom2() {
if let image2 = image2.image {
var minZoom = min(view.bounds.size.width / image2.size.width,
view.bounds.size.height / image2.size.height)
NSLog ("\(minZoom)")
if minZoom > 1 { minZoom = 1 }
scrollView.minimumZoomScale = minZoom
// Force scrollViewDidZoom fire if zoom did not change
if minZoom == lastZoomScale { minZoom += 0.000001 }
scrollView.zoomScale = minZoom
lastZoomScale = minZoom
}
}
@IBAction func onImageSizeToggleButtonTapped(sender: AnyObject) {
imageSizeToggleButton.selected = !imageSizeToggleButton.selected
imageSizeToggleButton.invalidateIntrinsicContentSize()
let fileName = imageSizeToggleButton.selected ?
imageScrollSmallImageName2 : imageScrollLargeImageName2
image.image = UIImage()
updateZoom()
updateZoom2()
}
// UIScrollViewDelegate
// -----------------------
func scrollViewDidZoom(scrollView: UIScrollView) {
updateConstraints()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return ??????
}}