【问题标题】:Add touch gesture to image and execute code为图像添加触摸手势并执行代码
【发布时间】:2017-04-30 18:21:43
【问题描述】:

我无法在下面的代码中向“thumbnailImageView”添加触摸手势,并使该点击手势执行与下面代码中的“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结束

【问题讨论】:

    标签: swift swift3 uiimage uitapgesturerecognizer


    【解决方案1】:

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

    尝试只创建一次视图,然后在 setupViews() 函数中配置它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多