- 1.单击:
- 2.长按:
- 3.轻扫手势:
- 4.拖拽手势:
- 5.捏合手势:
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
addLongPressGesture()
}
func addTapGesture()
{
let mainview = UIView(x: 40, y: 80, w: 334, h: 334)
mainview.backgroundColor = UIColor.orange
self.view.addSubview(mainview)
mainview.addTapGesture
{ (gesture) -> () in
print("---1.单击手势:view tapped")
}
}
func addLongPressGesture()
{
let mainview = UIView(x: 40, y: 120, w: 334, h: 334)
mainview.backgroundColor = UIColor.orange
self.view.addSubview(mainview)
mainview.addLongPressGesture
{ (long) -> () in
print("---2.长按手势:view tapped")
mainview.shakeViewForTimes(3)
mainview.addShadow(offset: CGSize(width:10, height:10) , radius: 10, color: .brown, opacity: 0.75)
}
}
}
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
addPinchGesture()
}
func addSwipeGesture()
{
let mainview = UIView(x: 40, y: 80, w: 334, h: 334)
mainview.backgroundColor = UIColor.orange
self.view.addSubview(mainview)
mainview.addSwipeGesture(direction: UISwipeGestureRecognizer.Direction.down)
{ (Swiped) -> () in
print("---滑动的方向 \(Swiped.direction)--")
print("---手指的数量 \(Swiped.numberOfTouchesRequired)--")
}
}
func addPanGesture()
{
let mainview = UIView(x: 40, y: 80, w: 150, h: 150)
mainview.backgroundColor = UIColor.orange
self.view.addSubview(mainview)
var originP = mainview.center
mainview.addPanGesture
{ (pan) -> () in
if(pan.state == .began)
{
originP = mainview.center
}
let distance = pan.translation(in: mainview)
let result = CGPoint(x: originP.x+distance.x, y: originP.y+distance.y)
mainview.center = result
}
}
func addPinchGesture()
{
let mainview = UIView(x: 40, y: 120, w: 334, h: 334)
mainview.backgroundColor = UIColor.orange
mainview.center = self.view.center
self.view.addSubview(mainview)
mainview.addPinchGesture
{ (pinch) -> () in
print(pinch.scale)
mainview.setScale(x: pinch.scale, y: pinch.scale)
}
}
}