如何将 UIGestureRecognizer 附加到 icarousel swift 代码中的项目视图
How to attach a UIGestureRecognizer to the item view in icarousel swift code
有人可以帮我在 icarousel 中为中心图像添加双击识别器吗?在下面的 swift 代码中,您可以看到已对识别器进行编程,但它会在屏幕上的任何地方做出反应,而不仅仅是在中心图像上。我想我必须将手势连接到 newView 但我尝试了几件事但没有解决问题。 Objective-C 中有很多示例,但是 Swift 很难在论坛上找到解决方案。希望有人能告诉我如何正确编码。
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
}
您是否尝试过将此代码放在 newView
初始化下方?
let tapGesture = UITapGestureRecognizer(target: self , action: "message")
tapGesture.numberOfTapsRequired = 2
newView.addGestureRecognizer(tapGesture)
您还应该删除 viewDidLoad()
中的那个手势。
编辑
您应该将识别器 放在 之后 newView
是 100% 而不是零,因为它现在在哪里,newView
可以是 nil
(你有一个if newView == nil
)
因此,将 if
语句置于所有语句下方。它应该工作。
有人可以帮我在 icarousel 中为中心图像添加双击识别器吗?在下面的 swift 代码中,您可以看到已对识别器进行编程,但它会在屏幕上的任何地方做出反应,而不仅仅是在中心图像上。我想我必须将手势连接到 newView 但我尝试了几件事但没有解决问题。 Objective-C 中有很多示例,但是 Swift 很难在论坛上找到解决方案。希望有人能告诉我如何正确编码。
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
}
您是否尝试过将此代码放在 newView
初始化下方?
let tapGesture = UITapGestureRecognizer(target: self , action: "message")
tapGesture.numberOfTapsRequired = 2
newView.addGestureRecognizer(tapGesture)
您还应该删除 viewDidLoad()
中的那个手势。
编辑
您应该将识别器 放在 之后 newView
是 100% 而不是零,因为它现在在哪里,newView
可以是 nil
(你有一个if newView == nil
)
因此,将 if
语句置于所有语句下方。它应该工作。