我已经提到了关于按下按钮的无数其他问题,但与Swift没有太多关联.我使用touchUpInside事件将一个功能连接到一个按钮:
@IBAction func singleFire(sender: AnyObject){
//code
}
Run Code Online (Sandbox Code Playgroud)
...和另一个功能,用于在按住相同按钮时重复调用上述功能,并在不再按下按钮时停止:
@IBAction func speedFire(sender: AnyObject){
button.addTarget(self, action: "buttonDown:", forControlEvents: .TouchDown)
button.addTarget(self, action: "buttonUp:", forControlEvents: .TouchUpOutside)
func buttonDown(sender: AnyObject){
timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: "singleFire", userInfo: nil, repeats: true)
}
func buttonUp(sender: AnyObject){
timer.invalidate()
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,我不知道如何将触摸事件设置为同一个按钮以实现不同的功能.
我遇到的问题是不能足够快地遍历UIImage中的所有像素,对照特定的一种(绿色)检查每种颜色。用户必须立即处理迭代。目前,存在约0.5秒的延迟。
我正在使用它来创建视图的屏幕截图:
func screenshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenShot!
}
Run Code Online (Sandbox Code Playgroud)
这将获得单个像素的颜色:
extension UIImage {
func getPixelColor(pos: CGPoint) -> UIColor {
let pixelData = self.cgImage!.dataProvider!.data
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4
let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
return UIColor(red: r, green: g, blue: …Run Code Online (Sandbox Code Playgroud) 我追求的效果:只有在滚动时才可见的图像之间的间距(如照片应用程序)。
许多旧的 obj-c 答案建议将滚动视图的边界扩展到屏幕外以使其页面更远,并使这个屏幕外空间成为图像之间的间隙。
pagingEnabled 的文档指出:
如果此属性的值为 YES,则当用户滚动时,滚动视图将在滚动视图边界的倍数处停止。
因此,在尝试更改倍数值时,我扩展了 scrollView 的宽度,并启用了左分页。然而,我没有实现页面超越差距的答案 - 他们总是把它留在视野中:
那么如果滚动宽度更长,为什么它不分页合适的距离呢?
let gapMargin = CGFloat(20)
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width + gapMargin, height: view.frame.height)
let exdScrollWidth = scrollView.frame.width
//1
let imageView1 = UIImageView()
imageView1.backgroundColor = UIColor.green
imageView1.frame = CGRect(x: 0, y: 0, width: exdScrollWidth - gapMargin, height: scrollView.bounds.size.height)
//2
let imageView2 = UIImageView()
imageView2.backgroundColor = UIColor.yellow
imageView2.frame = CGRect(x: exdScrollWidth, y: 0, width: exdScrollWidth - gapMargin, height: scrollView.bounds.size.height)
//3
let imageView3 = UIImageView()
imageView3.backgroundColor …Run Code Online (Sandbox Code Playgroud)