2017-06-20 1 views
0

私は画面の中央に150x150のメインImageviewを配置していますが、ランダムなxとyの値とランダムな幅と高さを持つビューをプログラムで作成しています値)。私はビューがメインのイメージビューを囲むが、それに触れないようにしたい。また、y軸には、self.view.frameと35の高さを持つタブバーがあり、これらのビューに触れたくない場合もあります。何か案は?UIViewsを触れないようにする方法

+0

は重複して作成されたビューですか? – BJHStudios

+0

私はむしろそうしたくないですが、ランダムなので避けるのは難しいかもしれません。 – andrewF

答えて

1

デカルト座標(xとy)の代わりに、極座標(角度と半径、参照を画像ビューの中心にする)をランダム化しないのはなぜですか?あなたが角度と半径を持っていれば、中心からの距離を制御する方が簡単になります。あなたのランダムな幅と高さがどのようになっているかに応じて半径を増やすことができます。そのため、衝突を補正し回避するために中心から離れています。

let centerImageView = UIImageView() 

//.. 

//Bounding circle for the image view 
let centerRadius = max(centerImageView.width, centerImageView.height) 

func randomFloat(min: Float, max: Float) { 
    return min + (max - min) * (arc4random()/RAND_MAX) 
} 

//Somewhere else in your loop for generating views 

let angle = randomFloat(0, Float.pi * 2) 
//Dimensions of the random view 
let width = randomFloat(min: 10, max: 50) 
let height = randomFloat(min: 10, max: 50) 
//Bounding circle, you can use an ellipse, or calculate the distance perfectly for that angle too, but this is simpler 
let viewRadius = randomFloat(min: width, max: height) 
//The distance from the center must be greater than the sum of both bounding circles' radiuses, plus some 
let radius = centerRadius + viewRadius + randomFloat(min: 50, max: 100) 
//Convert to cartesian coordinates 
let x = centerImageView.center.x + cos(angle) * radius 
let y = centerImageView.center.y + sin(angle) * radius 

let view = UIView(frame: CGRect(x: x, y: y, width: width, height: height) 

enter image description here

+0

返事ありがとうございます、残念ながらそれは私にとってはうまくいきませんでしたが、これに似た何かをcenterImageView? – andrewF

+0

あなたは何がうまくいかなかったかについてもっと具体的にすることができますか? –

+0

多くのキャストがあり、最終結果は表示されず、画面上に表示されませんでした – andrewF

0
func intersects(_ rect2: CGRect) -> Bool 

あなたの中心ビューは、新たなフレームを生成し、ランダムに、新しく作成されたビューで交差している場合。

関連する問題