Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在某个点创建UIView?_Ios_Swift_Uiview_Cgpoint - Fatal编程技术网

Ios 如何在某个点创建UIView?

Ios 如何在某个点创建UIView?,ios,swift,uiview,cgpoint,Ios,Swift,Uiview,Cgpoint,我需要能够在某个点创建和显示UIView 到目前为止,我有一个手势识别器,作为主视图的子视图添加 然后我通过编程创建一个UIView,并将它的x和y坐标设置为从手势识别器获得的CG点 我可以创建它并将其添加为子视图,但创建的UIView的位置与点击的位置不同 AnimationView子类UIView 我的代码在下面 tappedLocation = gesture.locationInView(self.view) var animationImage: AnimationV

我需要能够在某个点创建和显示UIView

到目前为止,我有一个手势识别器,作为主视图的子视图添加

然后我通过编程创建一个UIView,并将它的x和y坐标设置为从手势识别器获得的CG点

我可以创建它并将其添加为子视图,但创建的UIView的位置与点击的位置不同

AnimationView子类UIView

我的代码在下面

    tappedLocation = gesture.locationInView(self.view)

    var animationImage: AnimationView = AnimationView()
    animationImage.frame = CGRectMake(tappedLocation.x, tappedLocation.y, 64, 64)
    animationImage.contentMode = UIViewContentMode.ScaleAspectFill
    self.view.addSubview(animationImage)
    animationImage.addFadeAnimation(removedOnCompletion: true)

有什么我做错了吗?

您的问题是,您希望视图的中心是您单击的点。此时,
ui视图的左上角将是您触摸的点。因此,请尝试:

 var frameSize:CGFloat = 64
 animationImage.frame = CGRectMake(tappedLocation.x - frameSize/2, tappedLocation.y - frameSize/2, frameSize, frameSize)
如您所见,现在您可以在之前设置宽度和高度,并调整x和y,以便视图的中心是您所接触的点

但更好的方法是,就像Rob在回答中提到的那样,将视图的中心设置到您的位置。这样,您只需设置帧的大小,并使用
CGSizeMake
代替
CGRectMake
方法:

animationImage.frame.size = CGSizeMake(100, 100)
animationImage.center = tappedLocation

您的问题是,您希望视图的中心是您单击的点。此时,
ui视图的左上角将是您触摸的点。因此,请尝试:

 var frameSize:CGFloat = 64
 animationImage.frame = CGRectMake(tappedLocation.x - frameSize/2, tappedLocation.y - frameSize/2, frameSize, frameSize)
如您所见,现在您可以在之前设置宽度和高度,并调整x和y,以便视图的中心是您所接触的点

但更好的方法是,就像Rob在回答中提到的那样,将视图的中心设置到您的位置。这样,您只需设置帧的大小,并使用
CGSizeMake
代替
CGRectMake
方法:

animationImage.frame.size = CGSizeMake(100, 100)
animationImage.center = tappedLocation

只需设置其
中心

animationImage.center = tappedLocation

只需设置其
中心

animationImage.center = tappedLocation

让我们创建一个点击手势并将其指定给视图

let tapGesture = UITapGestureRecognizer()
tapGesture.addTarget(self, action: "tappedView:") // action is the call to the function that will be executed every time a Tap gesture gets recognised.
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
myView.addGestureRecognizer(tapGesture)
每次使用指定的点击手势点击视图时,都会调用此函数

func tappedView(sender: UITapGestureRecognizer) {
// Now you ca access all the UITapGestureRecognizer API and play with it however you want.

    // You want to center your view to the location of the Tap.
    myView.center = sender.view!.center

}

让我们创建一个点击手势并将其指定给视图

let tapGesture = UITapGestureRecognizer()
tapGesture.addTarget(self, action: "tappedView:") // action is the call to the function that will be executed every time a Tap gesture gets recognised.
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
myView.addGestureRecognizer(tapGesture)
每次使用指定的点击手势点击视图时,都会调用此函数

func tappedView(sender: UITapGestureRecognizer) {
// Now you ca access all the UITapGestureRecognizer API and play with it however you want.

    // You want to center your view to the location of the Tap.
    myView.center = sender.view!.center

}

你说的不同是什么意思?完全是另一个位置还是附近?经过仔细检查,它创建的视图的左角位于抽头的位置。我猜这就是水龙头的x+y坐标。如何使视图直接在水龙头的中心生成。你说的“不同”是什么意思?完全是另一个位置还是附近?经过仔细检查,它创建的视图的左角位于抽头的位置。我猜这就是水龙头的x+y坐标。如何使视图直接在水龙头的中心生成。我不知道。比只计算中间点好得多的方法+我不知道那件事。比只计算中间点好得多的方法+1.