有人可以幫我添加一個雙抽頭識別器在易拉寶的中心圖像?在下面的swift代碼中,您可以看到識別器已編程,但它在屏幕上的任何地方都會起作用,而不僅僅是在中央圖像上。我認爲我必須將手勢連接到newView,但我嘗試了幾件事情,但沒有解決問題。 Objective-C中有很多例子,但Swift很難在論壇上找到解決方案。希望有人能告訴我如何正確編寫代碼。如何將UIGestureRecognizer附加到易滑動快速代碼中的項目視圖
import UIKit
let image0 = UIImage(named: "Afrojack")
let image1 = UIImage(named: "Calvin Harris")
let image2 = UIImage(named: "Martin Garrix")
var imageArray = [image0, image1, image2]
class ViewController: UIViewController, iCarouselDataSource, iCarouselDelegate
{
var items: [Int] = []
@IBOutlet weak var carousel: iCarousel!
override func awakeFromNib()
{
super.awakeFromNib()
for i in 0...2
{
items.append(i)
}
}
override func viewDidLoad()
{
super.viewDidLoad()
carousel.type = .CoverFlow2
}
//show message when screen is tapped twice
func message(){
let alertView = UIAlertView(title: "Oops!", message: "Something happened...", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
func numberOfItemsInCarousel(carousel: iCarousel!) -> Int
{
return items.count
}
@objc func carousel(carousel: iCarousel!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! {
var newView = view
var label: UILabel! = nil
if newView == nil {
//create new view
//don't do anything specific to the index within
//this `if (view == nil) {...}` statement because the view will be
//recycled and used with other index values later
newView = UIImageView(frame:CGRectMake(0, 0, 200, 200))
//(newView as! UIImageView).image = UIImage(named: "page")
newView.contentMode = .Center
// set label properties
label = UILabel(frame:newView.bounds)
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.font = label.font.fontWithSize(50)
label.tag = 1
newView.addSubview(label)
}
else
{
//get a reference to the label in the recycled view
label = newView.viewWithTag(1) as! UILabel!
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
//label.text = "\(items[index])"
(newView as! UIImageView).image = imageArray[items[index]]
newView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self , action: "message")
tapGesture.numberOfTapsRequired = 2
newView.addGestureRecognizer(tapGesture)
return newView
}
}
func carousel(carousel: iCarousel!, valueForOption option: iCarouselOption, withDefault value: CGFloat) -> CGFloat
{
if (option == .Spacing)
{
return value * 1.1
}
return value
}
所以,你只需要在中心點,或者它可以在圖像上的任何地方雙擊? – FabKremer
我想在圖像上的雙選項卡(newView)在coverflow的中間。當圖像被雙擊時,我正在對另一個屏幕進行縮放,在這個屏幕中,我正在使用點擊圖像的數字來顯示屬於圖像的信息。認爲代碼必須放置在@obj有趣的傳送帶方法的if語句中,如標籤(我不使用它)。 –
**解決** 上面的代碼工作就像我會看到工作! FabKremer,感謝您的耐心和幫助! –