在我的视图内部,我有一个cardContainerView并附加了UIPanGestureRecognizer。根据用户是抓住卡片的上半部还是下半部,我想为卡片添加不同的旋转角度,现在可以正常使用了。
我的问题是我实现它的方式,用户可以抓住卡的顶部(导致正旋转)并将卡滑动到location.y阈值以下,然后导致卡使用负值旋转角度。这会引起卡的剧烈跳动。
我试图抓住.began部分中的location.y并将其分配给.changed,但显然我不能这样做。结果为零。有人知道我还能怎么解决吗?
非常感激!
//When animation begins
if sender.state == UIGestureRecognizerState.began {
print("PAN BEGAN")
cardContainerViewInitialCenter = cardContainerView.center
addNewTempCard()
initialPoint = location
print("InitialPoint PAN BEGAN \(initialPoint)")
print("InitialPoint.y PAN BEGAN \(initialPoint.y)")
}
//While panning
else if sender.state == UIGestureRecognizerState.changed {
print("PAN CHANGED")
//User grabbed card at top half
if location.y < view.frame.height/2 {
print("TOP HALF")
if translation.x >= 0 && translation.x <= 50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationTopPos)
tempCardView.alpha = convertedNewCardAlphaPos
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXPos, y: convertedNewCardScaleYPos)
}
else if translation.x > 50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationTopPos)
tempCardView.alpha = convertedNewCardAlphaPos
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXPos, y: convertedNewCardScaleYPos)
}
else if translation.x < 0 && translation.x >= -50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationTopNeg)
tempCardView.alpha = convertedNewCardAlphaNeg
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXNeg, y: convertedNewCardScaleYNeg)
}
else if translation.x < -50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationTopNeg)
tempCardView.alpha = convertedNewCardAlphaNeg
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXNeg, y: convertedNewCardScaleYNeg)
}
}
//User grabbed card at bottom half
else if location.y > view.frame.height/2 {
print("TOP HALF")
if translation.x >= 0 && translation.x <= 50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationBottomPos)
tempCardView.alpha = convertedNewCardAlphaPos
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXPos, y: convertedNewCardScaleYPos)
}
else if translation.x > 50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationBottomPos)
tempCardView.alpha = convertedNewCardAlphaPos
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXPos, y: convertedNewCardScaleYPos)
}
else if translation.x < 0 && translation.x >= -50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationBottomNeg)
tempCardView.alpha = convertedNewCardAlphaNeg
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXNeg, y: convertedNewCardScaleYNeg)
}
else if translation.x < -50 {
cardContainerView.center = CGPoint(x: cardContainerViewInitialCenter.x + translation.x, y: cardContainerViewInitialCenter.y)
cardContainerView.transform = CGAffineTransform.init(rotationAngle: convertedRotationBottomNeg)
tempCardView.alpha = convertedNewCardAlphaNeg
tempCardView.transform = view.transform.scaledBy(x: convertedNewCardScaleXNeg, y: convertedNewCardScaleYNeg)
}
}
}
最佳答案
我认为您应该将intialPoint设置为.began
状态。在.changed
中不应为零
//When animation begins
if sender.state == .began {
...
initialPoint = sender.location(in: self)
}
我也不认为
else
确实对您有帮助。它不应该是.began
,而不是.changed
,而是检查.began
和.changed
。关于ios - 从.began到.changed分配UIPanGestureRecognizer中的CGPoint,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47727021/