我有两个对象,第一个是ImageView,第二个是TextView。
我为这些物体制作了动画。如果用户单击图像,textView将展开以显示整个文本。但是,当textView展开时,imageView会显示奇怪的图像大小。看起来像是超级放大。
下面是我的代码
import UIKit类CollectionViewController: UICollectionViewController {
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!)
}}
发布于 2017-03-19 18:45:04
确保将ImageView内容模式设置为.scaleAspectFit或.scaleAspectFill
https://stackoverflow.com/questions/42884137
复制相似问题