简体   繁体   English

当我的UIView碰到窗口的边界时,会朝另一个方向进行动画处理

[英]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.

相关问题 UIView:在同一给定方向上无限地设置渐变动画? - UIView: animate a gradient inifinitely in the same given direction? 是否有关于UIView在窗口中的位置何时发生变化的通知? - Is there a notification for when a UIView's position in the window changes? UIView 动画不适用于 UIView 的图层 - UIView animate not working on UIView's layers 我使用UIView的“ animateWithDuration:animations:”方法来动画我的视图,它是底部的常量更改,但是它显示没有动画。 - I use the UIView `animateWithDuration:animations:` method to animate my view, it's bottom constant change, but it shows did not animate UIView Animate仅在位于IBAction中时才进行动画处理。 如何获得适合自己情况的动画? - UIView Animate only animates if it's in a IBAction. How do I get it to animate in my situation? 当UIVIew隐藏在sameView中时如何为UIview设置动画 - How to animate UIview when UIVIew is hidden in sameView 在所有其他视图之上对UIView进行动画处理 - Animate UIView above all other views 当使用 UIView.animate 动画视图时,其他视图也不必要地动画 - When animating view with UIView.animate other views are also needlessly animated iOS:检测我的 UIView 何时添加到其他视图中 - iOS: Detect when my UIView is add in other view 动画UIView的框架和角半径 - Animate UIView's frame and corner radius together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM