如何在视图外设置按钮? 因此,您可以在屏幕外生成唯一的坐标。我可以将按钮从该位置设置为中间位置。
我有这个代码来生成坐标
var p = CGPoint(x:Int(arc4random()%1000),y:Int(arc4random()%1000))
这是有效的,但我希望获得屏幕外的随机坐标,或者换句话说用户看到的内容。
答案 0 :(得分:1)
那样的事情呢?
func generateRandomPointOutsideOfViewBounds (view: UIView) -> CGPoint{
var min: UInt32 = UInt32(view.frame.size.width)
var max: UInt32 = UInt32(1000)
let randomX: UInt32 = arc4random_uniform(max - min) + min
min = UInt32(view.frame.size.height)
max = UInt32(1000)
let randomY: UInt32 = arc4random_uniform(max - min) + min
return CGPointMake(CGFloat(randomX), CGFloat(randomY))
}
答案 1 :(得分:0)
只需操纵frame
属性:
var myButton: UIButton // initialized somehow
// at appropriate time. Idiomatic place is in layoutSubviews() of your custom UIView subclass, but this may be triggered by an action, especially if you're following up immediately with animiation -- up to your design
myButton.frame = CGRect(p, myButton.frame.size)
// now call your animation
UIView.animateWithDuration(/* ... parameters ... */,
animations: { myButton.frame = /* resting location */ },
completion: { /* ... */ })
修改强>
看起来到目前为止的两个答案(美国东部时间8月17日12:46)涵盖了你问题的完全不同的方面,这显然是模棱两可的。他们一起回答了整个问题。