1.利用手勢滑動(dòng)UIPanGestureRecognizer 判斷手勢拖動(dòng)的坐標(biāo)
2.利用CATransform3DRotate實(shí)現(xiàn)拖動(dòng)過程圖片翻轉(zhuǎn)
3.利用手勢禁用范圍實(shí)現(xiàn)輪播圖scrollView翻頁
核心代碼
@objc fileprivate func panInCard(_ panGes: UIGestureRecognizer) {
let touchPoint = panGes.location(in: imageView)
if panGes.state == .changed {
UserDefaults.standard.set(true, forKey: IS_GESTURE_PAN)
NotificationCenter.default.post(name: PanInCardNotify, object: nil, userInfo: ["changed" : true])
let xFactor = myMIN(1, myMAX(-1, (touchPoint.x - (self.bounds.size.width / 2)) / (self.bounds.size.width / 2) ))
let yFactor = myMIN(1, myMAX(-1, (touchPoint.y - (self.bounds.size.height / 2)) / (self.bounds.size.height / 2) ))
imageView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
imageView.layer.transform = self.imagePanTransform(m34: 1.0 / CGFloat(-500), xf: xFactor, yf: yFactor)
// let zFactor = 180.0 * atan(yFactor / xFactor) / CGFloat(Double.pi) + 90
// printLog(zFactor)
} else if panGes.state == .ended {
NotificationCenter.default.post(name: PanInCardNotify, object: nil, userInfo: ["changed" : false])
UIView.animate(withDuration: kSpreadDuration, animations: {
self.imageView.layer.transform = CATransform3DIdentity
}, completion: nil)
}
}
fileprivate func imagePanTransform(m34: CGFloat, xf: CGFloat, yf: CGFloat) -> CATransform3D {
var t = CATransform3DIdentity
t.m34 = m34
t = CATransform3DRotate(t, CGFloat(Double.pi) / 15 * yf , -1, 0, 0)
//t: CATransform3D, t: CATransform3D, x: CGFloat, y: CGFloat, z: CGFloat 后面3個(gè)數(shù)字分別代表不同的軸來翻轉(zhuǎn)
// xf / 360.0 * 2.0 * CGFloat(Double.pi)
t = CATransform3DRotate(t, CGFloat(Double.pi) / 15 * xf , 0, 1, 0)
return t
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view == emptyView {
//#UIGestureRecognizerDelegate
//我在這里設(shè)置響應(yīng)事件的優(yōu)先級(jí),因?yàn)閁ITap響應(yīng)事件的優(yōu)先級(jí)會(huì)高兰伤,則他的subview的事件會(huì)被阻止
return false
}
return true
}
PanInCardNotify 通知的目的是在拖動(dòng)動(dòng)畫實(shí)現(xiàn)的過程時(shí)能夠清除輪播圖定時(shí)器和添加定時(shí)器