在我的项目中,用户可以UIView
(“CustomWishilstView”)如下:
func createCustomWishlistView() -> CustomWishlistView {
let v = CustomWishlistView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}
@IBAction func createListButtonTapped(_ sender: Any) {
// "Liste erstellen" button was tapped
self.appDidEnterBackgroundHandler()
if let txt = listNameTextfield.text {
self.newListTextfield.resignFirstResponder()
// append user-entered text to the data array
self.theData.append(txt)
self.imageData.append(self.image!)
let theCustomWishlistView = createCustomWishlistView()
self.view.addSubview(theCustomWishlistView)
// constrain CustomWishlistView
theCustomWishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 120.0).isActive = true
theCustomWishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
theCustomWishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0).isActive = true
theCustomWishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0).isActive = true
theCustomWishlistView.wishlistImage.image = self.image
theCustomWishlistView.wishlistLabel.text = txt
theCustomWishlistView.transform = CGAffineTransform(translationX: 0, y: 1000)
self.view.bringSubviewToFront(containerView)
// reload the collection view
theCollectionView.reloadData()
theCollectionView.performBatchUpdates(nil, completion: {
(result) in
// scroll to make newly added row visible (if needed)
let i = self.theCollectionView.numberOfItems(inSection: 0) - 1
let idx = IndexPath(item: i, section: 0)
self.theCollectionView.scrollToItem(at: idx, at: .bottom, animated: true)
// close (hide) the "New List" view
self.closeButtonTappedNewList(nil)
})
}
}
I transform
那view
所以一开始是看不见的。应该在用户单击cell
在一个UICollectionView
...我的代码(里面)cellForItemAt
)看起来是这样的:
if indexPath.item <= theData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = theData[indexPath.item - 1]
cell.buttonView.setImage(imageData[indexPath.item - 1], for: .normal)
cell.customWishlistTapCallback = {
// let CustomWishlistView appear
// UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
// theCustomWishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
// })
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
我的问题是我不能让我的customWishlistView
出现是因为我无法访问它。有办法解决这个问题吗?
你需要theCustomWishlistView一个实例变量,如下所示。您没有显示整个类代码,所以我的解决方案是尽可能完整的,而不需要看到其余的代码
class CustomViewController: UIViewController, UICollectionViewDataSource {
var theCustomWishlistView: UIView?
func createCustomWishlistView() -> CustomWishlistView {
let v = CustomWishlistView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}
@IBAction func createListButtonTapped(_ sender: Any) {
// "Liste erstellen" button was tapped
self.appDidEnterBackgroundHandler()
if let txt = listNameTextfield.text {
self.newListTextfield.resignFirstResponder()
// append user-entered text to the data array
self.theData.append(txt)
self.imageData.append(self.image!)
let wishlistView = createCustomWishlistView()
self.view.addSubview(wishlistView)
// constrain wishlistView
wishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 120.0).isActive = true
wishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
wishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0).isActive = true
wishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0).isActive = true
wishlistView.wishlistImage.image = self.image
wishlistView.wishlistLabel.text = txt
wishlistView.transform = CGAffineTransform(translationX: 0, y: 1000)
// retain wishlist view in instance variable
self.theCustomWishlistView = wishlistView
self.view.bringSubviewToFront(containerView)
// reload the collection view
theCollectionView.reloadData()
theCollectionView.performBatchUpdates(nil, completion: {
(result) in
// scroll to make newly added row visible (if needed)
guard let i = self.theCollectionView.numberOfItems(inSection: 0) else { return }
let idx = IndexPath(item: i - 1, section: 0)
self.theCollectionView.scrollToItem(at: idx, at: .bottom, animated: true)
// close (hide) the "New List" view
self.closeButtonTappedNewList(nil)
})
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return theData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let _cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath)
guard let cell = _cell as? ContentCell else { return _cell }
cell.testLabel.text = theData[indexPath.item - 1]
cell.buttonView.setImage(imageData[indexPath.item - 1], for: .normal)
cell.customWishlistTapCallback = {
// let CustomWishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.theCustomWishlistView?.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。