2017-04-30 16 views
0

아래 코드에서 "thumbnailImageView"에 터치 제스처를 추가하고 해당 탭 제스처가 아래 코드의 "func tapButton"과 동일한 코드를 수행하는 데 문제가 있습니다. 이 작업을 수행하기 위해 내가 변경해야 할 사항은 무엇입니까?이미지에 터치 제스처 추가 및 코드 실행

// 파일

// 파일 1의 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) 
} 

// 끝 파일이

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 
} 
} 

// 끝 파일이 셀에

답변

0

의, thumbnailImageView를 호출 할 때마다 새로운 View가 작성됩니다. 보기를 가져 오려고하면 셀에 추가 된보기 대신 새보기가 나타납니다.

보기를 한 번만 만든 다음 setupViews() 함수에서 구성하십시오.