我在swift中动画一些按钮可以永久地在视图中随机移动,但按钮的“目标”不会移动。要单击该按钮,您必须单击其创建的原始位置,即使它可能是屏幕的另一侧。
有没有人有关于如何使用按钮移动按钮目标的简单说明?
func circlePath(view: UIButton, pathDuration: Double){
//create an animation to follow a circular path
let pathAnimation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
//interpolate the movement to be more smooth
pathAnimation.calculationMode = kCAAnimationPaced
pathAnimation.repeatCount = .infinity
//no ease in/out to have the same speed along the path
pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
//the paths circular animation is proportional its size
pathAnimation.duration = pathDuration
//The circle to follow will be inside the circleContainer frame.
//it should be a frame around the center of your view to animate.
//do not make it to large, a width/height of 3-4 will be enough.
let curvedPath: CGMutablePathRef = CGPathCreateMutable()
let circleContainer: CGRect = CGRectInset(view.frame, 23, 23)
CGPathAddEllipseInRect(curvedPath, nil, circleContainer)
//add the path to the animation
pathAnimation.path = curvedPath
//add animation to the view's layer
view.layer.addAnimation(pathAnimation, forKey: "myCircleAnimation")
}
答案 0 :(得分:1)
不要使用术语" target"表示水龙头响应的区域。 "目标"关于按钮有一个特定的含义,它不是那个。
我们称之为"点击矩形"。
Core Animation实际上并不会在屏幕上为对象设置动画。它创建了一个"表示层"这样可以使对象的外观移动,但对象本身不会设置动画。
因此,在飞行途中"在飞行途中"在动画中。
相反,您需要做的是在超级视图上放置一个点击手势识别器(或者包含您要动画的图层的图层,如果您正在执行CAAnimation
。)然后您可以使用动画图层的表示层的hitTest方法来测试点击是否在被动画化的图层范围内。
我在github上有一个显示这种技术的项目。它被称为iOS-CAAnimation-group-demo(链接。)它是用Objective-C编写的,但无论语言如何,技术都是一样的,所以它应该让你知道如何继续。