我正在寻找一种简单的方法来处理 SKShapeNode 子类中的触摸。为此,我尝试使用touchesBegan,但它不起作用..
class Shape: SKShapeNode {
convenience init(rectOfSize: CGSize, colorOfRectangle rectColor: UIColor) {
self.init()
self.init(rectOfSize: rectOfSize)
self.fillColor = rectColor
self.position = CGPointMake(rectOfSize.width, rectOfSize.height)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touch")
}
}
Run Code Online (Sandbox Code Playgroud) 在我的函数中
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
for touch in touches{
let touchLocation = touch.location(in: self.view)
print("X: \(touchLocation.x) Y: \(touchLocation.y)")
}
}
Run Code Online (Sandbox Code Playgroud)
我得到反转的 Y 值。如果我点击顶部,我会得到 ~0 值,如果我点击底部,我会得到高价值。这使我的精灵在错误的 Y 方向上移动。
但是,如果我删除 self.view 中的“.view”,它会正常工作。有谁知道为什么当我使用 self.view 时它会反转?
因此,我将UIViewController子类化,在笔尖中有一个UIView,并在其中有一个tableview。据我了解,UIViewController和UIView都是UIResponder的子类,因此它们应该接收-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法。
但是,事实并非如此,并且我的视图控制器子类未接收该方法。如果可以的话,我真的不希望将UIView子类化。
我正在尝试实现这一点,但我的
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[textfield resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)
没有接到电话。
我有一个UIButton.当用户点击所述按钮时,如何获得手指的x坐标.我认为它将涉及touchesBegan,但如果按钮上出现"touchesBegan",我怎么才能获得值?如果有一种方法(不仅监视按钮的x,y,宽度和高度,并检查我的触摸是否在该区域)我将需要禁用按钮的用户交互,以便代码可以处理它而不是内置xib的东西.谢谢!
当我收到此错误时,我正在尝试创建一个新的覆盖功能:方法不会覆盖它的超类中的任何方法
我已经尝试删除覆盖部分,但这只会给我一个错误.
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch : UITouch! = touches.anyObject() as! UITouch
location = touch.locationInView(self.view)
person.center = location
}
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
let touch : UITouch! = touches.anyObject() as! UITouch
location = touch.locationInView(self.view)
person.center = location
}
Run Code Online (Sandbox Code Playgroud) 我有一个游戏,有3个SKSpriteNodes用户可以使用touchesBegan和移动touchesMoved.但是,当用户移动nodeA并通过另一个被调用的节点时nodeB,nodeB跟随nodeA等等.
我创建了一个数组SKSpriteNodes并使用a for-loop来简化生活.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let nodes = [nodeA, nodeB, nodeC]
for touch in touches {
let location = touch.location(in: self)
for node in nodes {
if node!.contains(location) {
node!.position = location
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除非在nodeA移动和交叉路径时nodeB,nodeB遵循nodeA.
我怎样才能让这个当用户移动nodeA并nodeA通过传递nodeB这nodeB将不遵循nodeA.
您好我有5张图片浏览,所有这些都是导航栏中的子视图.我想从每个imageview获得tapcounts,所以我认为我必须使用touchesBegan方法.但实际上,在每个图像视图中都没有检测到触摸事件.确切地说,touchesBegan在工具栏中检测到.层次结构是:
UIViewController - > UIToolbar
-> UINavigationBar -> UIImageView
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int indexSegment = _docSegmentedControl.selectedSegmentIndex;
CGRect frameTouch;
if (indexSegment == 0) {
frameTouch = frame;
}else if (indexSegment == 1) {
frameTouch = frame1;
}else if (indexSegment == 2) {
frameTouch = frame2;
}else if (indexSegment == 3) {
frameTouch = frame3;
}else if (indexSegment == 4) {
frameTouch = frame4;
}
UITouch *touch = [touches anyObject];
//--image 0--
CGPoint gestureStartPoint0 = [touch locationInView:imageView];
if (CGRectContainsPoint(frame, gestureStartPoint0)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的应用弹出一个弹出窗口。到目前为止,弹出窗口以固定的坐标弹出,我试图将其弹出到用户点击的位置。这就是我所拥有的:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesbegan")
for touch in touches{
//Handle touch
let location = touch.locationInView(self.view)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ColonyPopoverController") as! ColonyPopoverController
vc.modalPresentationStyle = .Popover
vc.preferredContentSize = CGSizeMake(200, 150)
if let popoverController = vc.popoverPresentationController {
popoverController.delegate = self
popoverController.sourceRect = CGRectMake(location.x, location.y, 20, 10)
popoverController.sourceView = self.view
self.presentViewController(vc, animated: true, completion: nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到当我点击模拟器时,打印语句永远不会打印。
我认为interaction并已multi-touch启用。我知道这种方法很好用,因为我也将其与Google Maps集成在一起,因此当我点击时,会出现一个google pin:
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) …Run Code Online (Sandbox Code Playgroud) 我在创建函数"touchesBegan"时遇到问题,然后创建一个包含x和y坐标的UIPoint和UITouch常量或变量.我在Objective-C中有我想要的确切代码,但我不知道它在Swift中的含义是什么.这是我想要基本上转换成Swift代码的Objective-C代码...注意:这是一个单视图应用程序,而不是游戏...提前感谢.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (point.x < 160) {
var = 10;
}
else{
var = 20;
}
}
Run Code Online (Sandbox Code Playgroud) touchesbegan ×9
swift ×5
ios ×4
sprite-kit ×3
ipad ×2
touchesmoved ×2
button ×1
gmsmapview ×1
iphone ×1
location ×1
objective-c ×1
skshapenode ×1
subclass ×1
touch ×1
uiimageview ×1
uitoolbar ×1
xcode ×1