gpt4 book ai didi

ios - 如何在 Swift/Xcode 中为 imageview 数组设置点击

转载 作者:行者123 更新时间:2023-11-29 01:50:17 25 4
gpt4 key购买 nike

我正在尝试为我的 imageview 数组中的每个 imageview 设置点击监听器,并在选择一个时打印。理想情况下,我希望每个 imageview 都有自己的监听器 - 例如“选择了 imageview 2”。谢谢!

class ViewController2: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

var index: Int = 0

var images: [UIImage] = [UIImage(named: "image1.png")!, UIImage(named: "image2.png")!, UIImage(named: "image3.png")!, UIImage(named: "image4.png")!, UIImage(named: "image5.png")!, UIImage(named: "image6.png")!, UIImage(named: "image7.png")!, UIImage(named: "image8.png")!, UIImage(named: "image9.png")!, UIImage(named: "image10.png")!, UIImage(named: "image11.png")!, UIImage(named: "image12.png")!, UIImage(named: "image13.png")!, UIImage(named: "image14.png")!, UIImage(named: "image15.png")!, UIImage(named: "image16.png")!, UIImage(named: "image17.png")!, UIImage(named: "image18.png")!, UIImage(named: "image19.png")!, UIImage(named: "image20.png")!, UIImage(named: "image21.png")!, UIImage(named: "image22.png")!, UIImage(named: "image23.png")!, UIImage(named: "image24.png")!, UIImage(named: "image25.png")!, UIImage(named: "image26.png")!, UIImage(named: "image27.png")!, UIImage(named: "image28.png")!, UIImage(named: "image29.png")!, UIImage(named: "image30.png")!]

var imageView: [UIImageView] = []



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
//let screenHeight = screenSize.height

let numWidth: CGFloat = 3
let numHeight: CGFloat = 10
self.scrollView.frame.size.width = screenWidth
let width: CGFloat = (self.scrollView.frame.size.width - (numWidth + 1))/3

var tap = UITapGestureRecognizer(target: self, action: Selector("tappedMe"))

for var i:CGFloat = 0; i < 3; ++i{
for var j:CGFloat = 0; j < 10; ++j {

let image: UIImage = images[index]
imageView.append(UIImageView(image: image))
imageView[index].frame = CGRectMake(width*i, width*j, width, width)
self.scrollView.addSubview(imageView[index])

imageView[index].addGestureRecognizer(tap)
imageView[index].userInteractionEnabled = true

index++
}
}
scrollView.contentSize.height = (width)*numHeight


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func imageTapped(img: AnyObject)
{
// Your action
print("tapped")
}


}

最佳答案

你应该使用自定义类而不是持有 UIImageView 数组。

 import UIKit 
class MyCustomImageView : UIImageView{

var myId : String = "";
override init(frame: CGRect){
super.init(frame: frame);
}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
println("IM TOUCHED\(myId)");
}
}

...

var imageView: [MyCustomImageView] = []

在你的循环中:

  for var i:CGFloat = 0; i < 3; ++i{
for var j:CGFloat = 0; j < 10; ++j {

let image: UIImage = images[index]
imageView.append(MyCustomImageView(frame: CGRectMake(width*i, width*j, width, width)))

imageView[index].image = image;
self.scrollView.addSubview(imageView[index])

imageView[index].myId = "Image number \(index)";


index++
}
}

顺便说一句,不知道你想用所有宽度高度做什么,但这是一种更清晰的方式:

class ViewController2: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

let imageIndex : (begin:Int,end:Int) = (begin:1,end:30);
var imageView: [MyCustomImageView] = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
//let screenHeight = screenSize.height

let numWidth: CGFloat = 3
let numHeight: CGFloat = 10
self.scrollView.frame.size.width = screenWidth
let width: CGFloat = (self.scrollView.frame.size.width - (numWidth + 1))/3
var index = self.imageIndex.begin;

for var i:CGFloat = 0; i < 3; ++i{
for var j:CGFloat = 0; j < 10; ++j {

let image: UIImage? = UIImage(named: "image\(index++).png");
var currentImageView = MyCustomImageView(frame: CGRectMake(width*i, width*j, width, width))
currentImageView.image = image;
currentImageView.userInteractionEnabled = true
currentImageView.myId = "image\(index++).png";
self.scrollView.addSubview(imageView[index])
imageView.append(currentImageView);
}
}
scrollView.contentSize.height = (width)*numHeight


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

关于ios - 如何在 Swift/Xcode 中为 imageview 数组设置点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31492696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com