【问题标题】:UIScrollView in UICollectionViewCell with SwiftUICollectionViewCell 中的 UIScrollView 与 Swift
【发布时间】:2015-04-27 17:53:34
【问题描述】:

我是 IOS 的新手。我尝试创建显示全屏大小的图像可以缩放的视图图像应用程序,当用户缩小小于全屏大小的图像时,图像返回全屏大小,我的想法是使用 UICollectionView 设置分页启用,滚动方向水平。所以我的FlowLayout会是这样的

var screenSize: CGRect!

 override func viewDidLoad() {
    super.viewDidLoad()

    screenSize = UIScreen.mainScreen().bounds

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: screenSize.width, height: screenSize.height)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

    self.collectionView!.collectionViewLayout = layout
    self.view.addSubview(collectionView!)

我的 CollectionViewCell 标识符名称“Cell”我将 ScrollView 拖入其中并将图像拖入该 ScrollView

我像这样在我的 CollectionViewCell 类中输出这两个项目

import UIKit

class MyCollectionCell: UICollectionViewCell, UIScrollViewDelegate{

@IBOutlet weak var scrollCell: UIScrollView!

@IBOutlet weak var imgTest: UIImageView! 

}

CollectionViewController 中的下一步设置我的虚拟图像是这样的

var imageName:[String] = ["audi.png", "benz.png", "flok.png", "audi.png", "benz.png", "flok.png", "audi.png", "benz.png", "flok.png"]

然后转到 cellForItemAtIndexPath

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionCell

    cell.layer.borderWidth = 0
    cell.frame.size.width = screenSize.width
    cell.frame.size.height = screenSize.height
    // set cell size become fullscreen

    cell.imgTest.image = UIImage(named: imageName[indexPath.row])
    cell.imgTest.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:cell.imgTest.image!.size)

    //call my dummy image to cell

    cell.scrollCell.addSubview(cell.imgTest)
    cell.scrollCell.contentSize = cell.imgTest.image!.size

    let scaleHeight = cell.scrollCell.frame.size.height / cell.scrollCell.contentSize.height
    let scaleWidth = cell.scrollCell.frame.size.width / cell.scrollCell.contentSize.width
    let minScale = min(scaleWidth, scaleHeight)
    cell.scrollCell.minimumZoomScale = minScale
    cell.scrollCell.maximumZoomScale = 1.0
    cell.scrollCell.zoomScale = minScale

    var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollviewDoubleTapped")
    doubleTapRecognizer.numberOfTapsRequired = 2
    doubleTapRecognizer.numberOfTapsRequired = 1
    cell.scrollCell.addGestureRecognizer(doubleTapRecognizer)

我运行这个项目,问题是

  1. 滚动大小不等于我的图像大小,但超过了
  2. 我的图片无法缩放(我已经勾选了“用户交互启用”)
  3. 我尝试使用捏合手势来解决它,但我不知道如何设置 minimumzoomscale 并且放大时图像无法滚动
  4. 通过此代码,我尝试将 json 中的图像与 SDWebImage 一起使用,我将 cellForItemAtIndexPath 中的代码从调用虚拟图像更改为此

    cell.imgTest.sd_setImageWithURL(NSURL(string: dataArray[indexPath.row]), placeholderImage: UIImage(named: "loading.png"))

但是这样线程 1:EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP,subcode=0x0)

  1. 我从这里去哪里,任何人都可以快速指导我

【问题讨论】:

标签: swift uiscrollview uicollectionviewcell pinchzoom sdwebimage


【解决方案1】:

使用滚动视图代替集合视图,更简单,更灵活。查看此链接非常好的教程。您可以合并滚动页面和滚动缩放。 UIScrollView Tutorial: Getting Started

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多