[英]Making my UIView animate in the other direction when it hits the window's bound
I was wondering if there was an easier way to allow my view to animate in the opposite direction when the frames of my view go off the window whilst animating. 我想知道是否有一种更简便的方法可以使我的视图在动画时从窗口离开窗口时以相反的方向进行动画处理。
I was thinking of doing something like: 我正在考虑做类似的事情:
let x = CGFloat(Int(arc4random_uniform(UInt32(self.view.frame.maxX))))
let y = CGFloat(Int(arc4random_uniform(UInt32(self.view.frame.maxY))))
let label = UILabel(frame: CGRectMake(x,y, 50, 50))
label.text = "omnomnom"
label.sizeToFit()
let animateTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(animateLabel), userInfo: nil, repeats: true)
Where animateLabel
is: animateLabel
在哪里:
// Assuming that I have an instance variable of the label
if(label.layer.presentationLayer()?.frame.minX <= self.frame.minX
&& label.layer.presentationLayer()?.frame.maxY <= self.frame.maxY) {
// Then grow in every direction but the left direction
}
else if (label.layer.presentationLayer()?.frame.minX <= self.frame.minX) {
// Then grow in every direction but the left direction bottom direction
}
...
But which CGAfflineTransform
should I be using to specify transforms in these direction there are so many but I can't seem to find the correct one. 但是我应该使用哪个
CGAfflineTransform
来指定在这些方向上的转换,但是有很多,但是我似乎找不到正确的转换。
Also, is there a cleaner way to do this via some Core Animation API? 另外,是否有更清洁的方法可以通过某些Core Animation API做到这一点? Or is this the only way?
还是这是唯一的方法?
EDIT: So it seems like my question was misinterpreted, I want the view to grow as in doing something like CGAfflineTransformMakeScale(2,2)
. 编辑:所以似乎我的问题被误解了,我希望视图像做
CGAfflineTransformMakeScale(2,2)
一样CGAfflineTransformMakeScale(2,2)
。 But doing this would result to the label going off screen if it appeared near the edge of the window. 但是,如果标签出现在窗口边缘附近,则会导致标签离开屏幕。 So I was wondering whether there is a CG API that I could use so that I wouldn't have to check the frame of my label every time before determining which way it should grow
所以我想知道是否可以使用CG API,这样我就不必在每次确定标签增长方式之前都要检查标签的框架
I would suggest doing a UIView keyframe animation. 我建议做一个UIView关键帧动画。 (Using the UIView
animateKeyframesWithDuration:delay:options:animations:completion:
method.) (使用UIView
animateKeyframesWithDuration:delay:options:animations:completion:
方法。)
You could set up a series of keyframes that would move your view to first one edge of the window, then the other. 您可以设置一系列关键帧,将视图移动到窗口的第一个边缘,然后移动到另一个边缘。
I have a project called KeyframeViewAnimations on Github that demonstrates both UIView
keyframe animation and the equivalent Core Animation technique. 我在Github上有一个名为KeyframeViewAnimations的项目,该项目演示了
UIView
关键帧动画和等效的Core Animation技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.