首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift 3和UIImageview案例

swift 3和UIImageview案例
EN

Stack Overflow用户
提问于 2017-03-19 15:41:51
回答 1查看 52关注 0票数 0

我有两个对象,第一个是ImageView,第二个是TextView。

我为这些物体制作了动画。如果用户单击图像,textView将展开以显示整个文本。但是,当textView展开时,imageView会显示奇怪的图像大小。看起来像是超级放大。

This picture to make it clear

下面是我的代码

代码语言:javascript
复制
import UIKit

类CollectionViewController: UICollectionViewController {

代码语言:javascript
复制
var imageViewArray = [UIImage]()
var textArray = [String]()




override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    imageViewArray = [#imageLiteral(resourceName: "a"),#imageLiteral(resourceName: "b"),#imageLiteral(resourceName: "c")]


    textArray = [
        // firststory Starts
        "firststory",

                 // SecondStory starts
                 "Hello",

                 // thirdStors starts
                 "thirdStory"]

}




override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageViewArray.count

}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell

    // give the objects their IDs

    let imageView = cell.viewWithTag(1) as! UIImageView
    imageView.image = imageViewArray[indexPath.row]


    let textView = cell.viewWithTag(2)  as! UITextView
    textView.isScrollEnabled = false
    textView.text = textArray[indexPath.row]




    let backButton = cell.viewWithTag(3) as! UIButton
    backButton.isHidden = true



    return cell
}


// adding some animation to the cells

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)

    cell?.superview?.bringSubview(toFront: cell!)



    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: ({



        cell?.frame = collectionView.bounds
        collectionView.isScrollEnabled = false

        let textView = cell!.viewWithTag(2)  as! UITextView
        textView.isScrollEnabled = false


        // some codes to help back button to work !
        let backButton = cell!.viewWithTag(3) as! UIButton
        backButton.isHidden = false
        backButton.addTarget(self, action: #selector(CollectionViewController.backbtnAction), for: UIControlEvents.touchUpInside)



    }), completion: nil)



}


// back button

func backbtnAction() {

    let indexPath = collectionView?.indexPathsForSelectedItems
    collectionView?.isScrollEnabled = true
    collectionView?.reloadItems(at: indexPath!)
}

}

EN

回答 1

Stack Overflow用户

发布于 2017-03-19 18:45:04

确保将ImageView内容模式设置为.scaleAspectFit或.scaleAspectFill

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42884137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档