将触摸手势添加到图像并执行代码



我很难在下面的代码中添加触摸手势,并在下面的代码中执行与" func tapbutton"相同的代码。我应该做些什么来执行此操作?

//文件1

import UIKit
class BookCell: BaseCell{

var book: Book?{
didSet{
    thumbnailImageView.image = UIImage(named:  (book?.thumbnailImageName)!)
    titleLabel.text = book?.title
    subtitleTextView.text = book?.subTitle   
}
}

var thumbnailImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "")
imageView.isUserInteractionEnabled = true
imageView.tag = 0
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
}()

let userProfileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "Gary Vee Profile Pic 1")
imageView.layer.cornerRadius = 22
imageView.layer.masksToBounds = true
return imageView
}()
let button = UIButton(type: .system) // let preferred over var here
button.frame = CGRect(x: 200, y: 239, width: 130, height: 30)
button.backgroundColor = UIColor(red: 236/255, green: 63/255, blue: 53/255, alpha: 1)
button.setTitle("Buy Now", for: UIControlState())
button.setTitleColor(UIColor .white, for: UIControlState.normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
return button
}()

override func setupViews(){
addSubview(thumbnailImageView)
addSubview(separatorView)
addSubview(userProfileImageView)
addSubview(titleLabel)
addSubview(subtitleTextView)
addSubview(purchaseButton)
}

//文件结尾1

//文件2

import Foundation
import SafariServices
class Books : UICollectionViewController, UICollectionViewDelegateFlowLayout {
var books: [Book] = {

var book1 = Book()
book1.thumbnailImageName = "book 1"
book1.title = "Swift 3"
book1.subTitle = "by Author"
book1.url = "http://www.barnesandnoble.com"

return[book1]
}()

override func viewDidLoad() {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
navigationItem.title = "Books"
collectionView!.backgroundColor = UIColor.white
collectionView?.register(BookCell.self, forCellWithReuseIdentifier:"cellId")

}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return books.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BookCell
cell.book = books[indexPath.item]
cell.purchaseButton.tag = indexPath.row + 500
cell.purchaseButton.addTarget(self, action: #selector(tapButton), for: .touchUpInside)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
cell.thumbnailImageView.addGestureRecognizer(tapGestureRecognizer)
return cell
}

func tapButton(_ sender: UIButton) {
let index = sender.tag - 500
print("Please Help! (books[index].url ?? "")")
let mapUrl = books[index].url ?? "http://www.google.com" //if url is black it will take to google
let svc = SFSafariViewController(url: URL(string: mapUrl)!,entersReaderIfAvailable: true)
self.present(svc, animated: true, completion: nil)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = (view.frame.width - 16 - 16) * 9 / 16
return CGSize(width: view.frame.width, height: height + 16 + 68)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}

//文件结束2

在您的单元格中,每个呼叫thumbnailimageView都会创建一个新视图。因此,当您尝试获取视图时,您将获得新的视图,而不是添加到单元格的视图。

尝试仅创建一次视图,然后在setupviews((函数上配置它们。

最新更新