TouchesBegan always returns wrong UIView
I want to detect when I'm touching somewhere else rather than a specific UIView
. I'm using touchesBegan
for it, however, it always prints "touch is outside" (see code below). What am I missing?
I got help from this post.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
if let touch = touches.first
let hitView = self.view.hitTest(touch.location(in: self.view), with: event)
if hitView === checkBackContainer
print("touch is inside")
else
print("touch is outside")
super.touchesBegan(touches, with: event)
Anchors added inside ViewDidLoad
Function
private lazy var checkBackContainer = ImageUploadContainerView()
override func viewDidLoad()
self.view.addSubview(checkBackContainer)
checkBackContainer.anchorCenterXToSuperview()
checkBackContainer.topAnchor.constraint(equalTo: checkFrontContainer.bottomAnchor, constant: 20).isActive = true
checkBackContainer.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 25).isActive = true
checkBackContainer.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -25).isActive = true
checkBackContainer.heightAnchor.constraint(equalTo: checkBackContainer.widthAnchor, multiplier: 0.42).isActive = true
checkBackContainer.layer.applySketchShadow(color: UIColor.white, alpha: 1.0, x: 0, y: 0.33, blur: 1, spread: 0, cornerRadius: 6)
let backTap = UITapGestureRecognizer(target: self, action: #selector(self.backContainerTapped(_:)))
checkBackContainer.addGestureRecognizer(backTap)
EDIT: ContainerView is a custom UIView
which has some UIStackView
s, UIlabel
s and UIImageView
s in it. I found that It is because a custom UIView
, when I change it with a regular UIView
. It is working.
ios swift uiview uikit
add a comment |
I want to detect when I'm touching somewhere else rather than a specific UIView
. I'm using touchesBegan
for it, however, it always prints "touch is outside" (see code below). What am I missing?
I got help from this post.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
if let touch = touches.first
let hitView = self.view.hitTest(touch.location(in: self.view), with: event)
if hitView === checkBackContainer
print("touch is inside")
else
print("touch is outside")
super.touchesBegan(touches, with: event)
Anchors added inside ViewDidLoad
Function
private lazy var checkBackContainer = ImageUploadContainerView()
override func viewDidLoad()
self.view.addSubview(checkBackContainer)
checkBackContainer.anchorCenterXToSuperview()
checkBackContainer.topAnchor.constraint(equalTo: checkFrontContainer.bottomAnchor, constant: 20).isActive = true
checkBackContainer.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 25).isActive = true
checkBackContainer.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -25).isActive = true
checkBackContainer.heightAnchor.constraint(equalTo: checkBackContainer.widthAnchor, multiplier: 0.42).isActive = true
checkBackContainer.layer.applySketchShadow(color: UIColor.white, alpha: 1.0, x: 0, y: 0.33, blur: 1, spread: 0, cornerRadius: 6)
let backTap = UITapGestureRecognizer(target: self, action: #selector(self.backContainerTapped(_:)))
checkBackContainer.addGestureRecognizer(backTap)
EDIT: ContainerView is a custom UIView
which has some UIStackView
s, UIlabel
s and UIImageView
s in it. I found that It is because a custom UIView
, when I change it with a regular UIView
. It is working.
ios swift uiview uikit
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40
add a comment |
I want to detect when I'm touching somewhere else rather than a specific UIView
. I'm using touchesBegan
for it, however, it always prints "touch is outside" (see code below). What am I missing?
I got help from this post.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
if let touch = touches.first
let hitView = self.view.hitTest(touch.location(in: self.view), with: event)
if hitView === checkBackContainer
print("touch is inside")
else
print("touch is outside")
super.touchesBegan(touches, with: event)
Anchors added inside ViewDidLoad
Function
private lazy var checkBackContainer = ImageUploadContainerView()
override func viewDidLoad()
self.view.addSubview(checkBackContainer)
checkBackContainer.anchorCenterXToSuperview()
checkBackContainer.topAnchor.constraint(equalTo: checkFrontContainer.bottomAnchor, constant: 20).isActive = true
checkBackContainer.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 25).isActive = true
checkBackContainer.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -25).isActive = true
checkBackContainer.heightAnchor.constraint(equalTo: checkBackContainer.widthAnchor, multiplier: 0.42).isActive = true
checkBackContainer.layer.applySketchShadow(color: UIColor.white, alpha: 1.0, x: 0, y: 0.33, blur: 1, spread: 0, cornerRadius: 6)
let backTap = UITapGestureRecognizer(target: self, action: #selector(self.backContainerTapped(_:)))
checkBackContainer.addGestureRecognizer(backTap)
EDIT: ContainerView is a custom UIView
which has some UIStackView
s, UIlabel
s and UIImageView
s in it. I found that It is because a custom UIView
, when I change it with a regular UIView
. It is working.
ios swift uiview uikit
I want to detect when I'm touching somewhere else rather than a specific UIView
. I'm using touchesBegan
for it, however, it always prints "touch is outside" (see code below). What am I missing?
I got help from this post.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
if let touch = touches.first
let hitView = self.view.hitTest(touch.location(in: self.view), with: event)
if hitView === checkBackContainer
print("touch is inside")
else
print("touch is outside")
super.touchesBegan(touches, with: event)
Anchors added inside ViewDidLoad
Function
private lazy var checkBackContainer = ImageUploadContainerView()
override func viewDidLoad()
self.view.addSubview(checkBackContainer)
checkBackContainer.anchorCenterXToSuperview()
checkBackContainer.topAnchor.constraint(equalTo: checkFrontContainer.bottomAnchor, constant: 20).isActive = true
checkBackContainer.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 25).isActive = true
checkBackContainer.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -25).isActive = true
checkBackContainer.heightAnchor.constraint(equalTo: checkBackContainer.widthAnchor, multiplier: 0.42).isActive = true
checkBackContainer.layer.applySketchShadow(color: UIColor.white, alpha: 1.0, x: 0, y: 0.33, blur: 1, spread: 0, cornerRadius: 6)
let backTap = UITapGestureRecognizer(target: self, action: #selector(self.backContainerTapped(_:)))
checkBackContainer.addGestureRecognizer(backTap)
EDIT: ContainerView is a custom UIView
which has some UIStackView
s, UIlabel
s and UIImageView
s in it. I found that It is because a custom UIView
, when I change it with a regular UIView
. It is working.
ios swift uiview uikit
ios swift uiview uikit
edited Nov 14 '18 at 11:31
Emre Önder
asked Nov 14 '18 at 10:26
Emre ÖnderEmre Önder
813524
813524
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40
add a comment |
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40
add a comment |
1 Answer
1
active
oldest
votes
Try adding UIGestureRecognizer to the views you want to be detected on touch:
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
view1.addGestureRecognizer(tap)
view2.addGestureRecognizer(tap)
@objc func didTap(sender: UITapGestureRecognizer)
//Perform whatever you want in here
Alternatively, you can loop through all the subviews of parent view and exclude whatever views you want to exclude, like so:
for view in self.view.subviews
if view != viewYouWantToExclude
view.addGestureRecognizer(tap)
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297970%2ftouchesbegan-always-returns-wrong-uiview%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try adding UIGestureRecognizer to the views you want to be detected on touch:
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
view1.addGestureRecognizer(tap)
view2.addGestureRecognizer(tap)
@objc func didTap(sender: UITapGestureRecognizer)
//Perform whatever you want in here
Alternatively, you can loop through all the subviews of parent view and exclude whatever views you want to exclude, like so:
for view in self.view.subviews
if view != viewYouWantToExclude
view.addGestureRecognizer(tap)
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
add a comment |
Try adding UIGestureRecognizer to the views you want to be detected on touch:
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
view1.addGestureRecognizer(tap)
view2.addGestureRecognizer(tap)
@objc func didTap(sender: UITapGestureRecognizer)
//Perform whatever you want in here
Alternatively, you can loop through all the subviews of parent view and exclude whatever views you want to exclude, like so:
for view in self.view.subviews
if view != viewYouWantToExclude
view.addGestureRecognizer(tap)
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
add a comment |
Try adding UIGestureRecognizer to the views you want to be detected on touch:
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
view1.addGestureRecognizer(tap)
view2.addGestureRecognizer(tap)
@objc func didTap(sender: UITapGestureRecognizer)
//Perform whatever you want in here
Alternatively, you can loop through all the subviews of parent view and exclude whatever views you want to exclude, like so:
for view in self.view.subviews
if view != viewYouWantToExclude
view.addGestureRecognizer(tap)
Try adding UIGestureRecognizer to the views you want to be detected on touch:
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(sender:)))
view1.addGestureRecognizer(tap)
view2.addGestureRecognizer(tap)
@objc func didTap(sender: UITapGestureRecognizer)
//Perform whatever you want in here
Alternatively, you can loop through all the subviews of parent view and exclude whatever views you want to exclude, like so:
for view in self.view.subviews
if view != viewYouWantToExclude
view.addGestureRecognizer(tap)
answered Nov 14 '18 at 11:34
KLDKLD
5125
5125
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
add a comment |
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
My problem is that I want to detect the touch outside of the view. With your code, I can only detect the view not outside of it.
– Emre Önder
Nov 14 '18 at 11:35
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
Exactly, if you add gesture recognizer to all the views except the view you want to exclude, that is the functionality that you will get. It's arguably not the best approach, but it will definitely work.
– KLD
Nov 14 '18 at 11:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297970%2ftouchesbegan-always-returns-wrong-uiview%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
try to add anchors in viewDidLayoutSubviews ... see if makes a difference. But I suspect checkBackContainer.anchorCenterXToSuperview() is the culprit anyway. Also try to bring checkBackContainer to front.
– user3344236
Nov 14 '18 at 10:40
I also tried to give position with CGRect. When I do that , just a little point of the UIView detect the inside.
– Emre Önder
Nov 14 '18 at 10:49
are the events you are getting belonging the child controls of the container view?
– dniswhite
Nov 14 '18 at 22:40