Ios pageCurl动画与触摸交互
我正在寻找一种方法来指示Ios pageCurl动画与触摸交互,ios,swift,animation,touch,page-curl,Ios,Swift,Animation,Touch,Page Curl,我正在寻找一种方法来指示UIView上与触摸交互的页面卷曲动画(开始/移动/结束) 注: 我不能使用UIPageController。动画应应用于ui视图 动画与触摸开始、触摸运动和触摸结束之间的交互非常重要。和视频一模一样 示例代码,但不适用于触摸屏: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. BackBtn
UIView
上与触摸交互的页面卷曲动画(开始/移动/结束)
注:
ui视图
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
BackBtn.layer.cornerRadius = 5.0
AnimationBtn.layer.cornerRadius = 5.0
transition.delegate = self
transition.duration = 1.5
transition.startProgress = 0
transition.endProgress = 1
transition.type = CATransitionType(string: "pageCurl") as String
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.fillMode = kCAFillModeBoth
transition.isRemovedOnCompletion = false
}
联系方式:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let touch = touches.first {
let endPosition = touch.location(in: touch.view)
differenceX = startPosition.x - endPosition.x
differenceY = endPosition.y - startPosition.y
transition.subtype = kCATransitionFromRight
touch.view!.layer.add(transition, forKey: kCATransition)
webView.scrollView.contentOffset = CGPoint(x: nextPage + differenceX, y: 0)
}
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
如果让触摸=先触摸{
让endPosition=touch.location(在:touch.view中)
差分x=起始位置.x-结束位置.x
差分y=结束位置.y-开始位置.y
transition.subtype=kCATransitionFromRight
touch.view!.layer.add(转换,forKey:kCATransition)
webView.scrollView.contentOffset=CGPoint(x:nextPage+differenceX,y:0)
}
}
我找到了答案。我只需要更改转换.startProgress
和转换.endProgress
示例代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
calculatePageCount()
if let touch = touches.first {
startPosition = touch.location(in: self.view)
endPosition = startPosition
previousPositionX = startPosition.x
print("start x: \(startPosition.x), start y: \(startPosition.y)")
transition.startProgress = 0.0
if index == 0 && startPosition.x > (screenWidth / 2) {
transition.endProgress = 1.0 - Float(startPosition.x / screenWidth)
transition.subtype = kCATransitionFromRight
self.view.layer.add(transition, forKey: kCATransition)
} else if index != 0 {
if index != (pageCounter - 1) && startPosition.x > (screenWidth / 2) {
if self.view.layer.animationKeys() != nil {
self.view.layer.removeAllAnimations()
}
transition.endProgress = 1.0 - Float(startPosition.x / screenWidth)
transition.subtype = kCATransitionFromRight
self.view.layer.add(transition, forKey: kCATransition)
} else if startPosition.x < (screenWidth / 2) {
if self.view.layer.animationKeys() != nil {
self.view.layer.removeAllAnimations()
}
transition.endProgress = Float(startPosition.x / screenWidth)
transition.subtype = kCATransitionFromLeft
self.view.layer.add(transition, forKey: kCATransition)
} else {
if self.view.layer.animationKeys() != nil {
self.view.layer.removeAllAnimations()
}
}
} else {
if self.view.layer.animationKeys() != nil {
self.view.layer.removeAllAnimations()
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let touch = touches.first {
endPosition = touch.location(in: touch.view)
print("end x: \(endPosition.x), end y: \(endPosition.y)")
differenceX = startPosition.x - endPosition.x
differenceY = endPosition.y - startPosition.y
let differencePosition = endPosition.x - previousPositionX
previousPositionX = endPosition.x
print("difference x: \(differenceX)")
if self.view.layer.animationKeys() != nil {
transition.startProgress = transition.endProgress
if differenceX > 0 {
differencePosition < 0 ? (transition.endProgress = transition.endProgress + Float((abs(differencePosition) / screenWidth))) : (transition.endProgress = transition.endProgress - Float((abs(differencePosition) / screenWidth)))
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
} else {
differencePosition > 0 ? (transition.endProgress = transition.endProgress + Float((abs(differencePosition) / screenWidth))) : (transition.endProgress = transition.endProgress - Float((abs(differencePosition) / screenWidth)))
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
print("touch ended")
if self.view.layer.animationKeys() != nil {
if startPosition.x == endPosition.x {
transition.startProgress = transition.endProgress
transition.endProgress = 0
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
} else {
if differenceX > 0 {
if differenceX > (screenWidth / 2) {
if index < (pageCounter - 1) {
index += 1
}
transition.startProgress = transition.endProgress
transition.endProgress = 1
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
} else {
transition.endProgress = 0
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
}
} else {
if abs(differenceX) > (screenWidth / 2) {
if index > 0 {
index -= 1
transition.startProgress = transition.endProgress
transition.endProgress = 1
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
} else {
transition.endProgress = 0
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
}
} else {
transition.endProgress = 0
self.view.layer.removeAllAnimations()
self.view.layer.add(transition, forKey: kCATransition)
}
}
}
}
nextPage = CGFloat(index) * webView.scrollView.bounds.size.width
webView.scrollView.contentOffset = CGPoint(x: nextPage, y: 0)
}
func calculatePageCount() {
pageCounter = Int((webView.scrollView.contentSize.width / webView.scrollView.bounds.size.width).rounded())
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
calculatePageCount()
如果让触摸=先触摸{
startPosition=touch.location(在:self.view中)
结束位置=开始位置
previousPositionX=startPosition.x
打印(“开始x:\(startPosition.x),开始y:\(startPosition.y)”)
transition.startProgress=0.0
如果索引==0&&startPosition.x>(屏幕宽度/2){
transition.endProgress=1.0-浮动(startPosition.x/屏幕宽度)
transition.subtype=kCATransitionFromRight
self.view.layer.add(transition,forKey:kCATransition)
}否则,如果索引!=0{
如果索引!=(pageCounter-1)和&startPosition.x>(屏幕宽度/2){
如果self.view.layer.animationKeys()!=nil{
self.view.layer.RemoveAllianimations()
}
transition.endProgress=1.0-浮动(startPosition.x/屏幕宽度)
transition.subtype=kCATransitionFromRight
self.view.layer.add(transition,forKey:kCATransition)
}如果startPosition.x<(屏幕宽度/2),则为else{
如果self.view.layer.animationKeys()!=nil{
self.view.layer.RemoveAllianimations()
}
transition.endProgress=Float(startPosition.x/屏幕宽度)
transition.subtype=kCATransitionFromLeft
self.view.layer.add(transition,forKey:kCATransition)
}否则{
如果self.view.layer.animationKeys()!=nil{
self.view.layer.RemoveAllianimations()
}
}
}否则{
如果self.view.layer.animationKeys()!=nil{
self.view.layer.RemoveAllianimations()
}
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
super.touchesMoved(touches,with:event)
如果让触摸=先触摸{
endPosition=touch.location(在:touch.view中)
打印(“结束x:\(endPosition.x),结束y:\(endPosition.y)”)
差分x=起始位置.x-结束位置.x
差分y=结束位置.y-开始位置.y
让differencePosition=endPosition.x-previousPositionX
previousPositionX=endPosition.x
打印(“差异x:\(差异x)”)
如果self.view.layer.animationKeys()!=nil{
transition.startProgress=transition.endProgress
如果差值x>0{
差异位置<0?(transition.endProgress=transition.endProgress+Float((abs(差异位置)/屏幕宽度)):(transition.endProgress=transition.endProgress-Float((abs(差异位置)/屏幕宽度)))
self.view.layer.RemoveAllianimations()
self.view.layer.add(transition,forKey:kCATransition)
}否则{
differencePosition>0?(transition.endProgress=transition.endProgress+Float((abs(differencePosition)/screenWidth)):(transition.endProgress=transition.endProgress-Float((abs(differencePosition)/screenWidth)))
self.view.layer.RemoveAllianimations()
self.view.layer.add(transition,forKey:kCATransition)
}
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
super.touchesend(触摸,带有:事件)
打印(“触摸结束”)
如果self.view.layer.animationKeys()!=nil{
如果startPosition.x==endPosition.x{
transition.startProgress=transition.endProgress
transition.endProgress=0
self.view.layer.RemoveAllianimations()
self.view.layer.add(transition,forKey:kCATransition)
}否则{
如果差值x>0{
如果差异X>(屏幕宽度/2){
如果索引<(分页计数器-1){
指数+=1
}
transition.startProgress=transition.endProgress
transition.endProgress=1
self.view.layer.RemoveAllianimations()
self.view.layer.add(transition,forKey:kCATransition)
}否则{
transition.endProgress=0
self.view.layer.RemoveAllianimations()
self.view.layer.add(transition,forKey:kCATransition)
}
}否则{
如果abs(差异x)>(屏幕宽度/2){
如果索引>0{
索引-=1
transition.startProgress=transition.endProgress
transition.endProgress=1
self.view.layer.removeAllAnimatio