Detect tap on specific part of UIButton
I'm developing an instagram clone and I'm trying to dealing with user interact with a photo caption feature in Home Feed
screen.
I want if a user tap on username
, controller will push ProfileViewController
or if user tap on caption
, controller will push CommentsViewController
.
Thanks for any suggest!
ios swift instagram
add a comment |
I'm developing an instagram clone and I'm trying to dealing with user interact with a photo caption feature in Home Feed
screen.
I want if a user tap on username
, controller will push ProfileViewController
or if user tap on caption
, controller will push CommentsViewController
.
Thanks for any suggest!
ios swift instagram
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
You can follow this answer
– TheTiger
Jan 2 at 4:56
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42
add a comment |
I'm developing an instagram clone and I'm trying to dealing with user interact with a photo caption feature in Home Feed
screen.
I want if a user tap on username
, controller will push ProfileViewController
or if user tap on caption
, controller will push CommentsViewController
.
Thanks for any suggest!
ios swift instagram
I'm developing an instagram clone and I'm trying to dealing with user interact with a photo caption feature in Home Feed
screen.
I want if a user tap on username
, controller will push ProfileViewController
or if user tap on caption
, controller will push CommentsViewController
.
Thanks for any suggest!
ios swift instagram
ios swift instagram
edited Jan 2 at 14:04
andesta.erfan
5421323
5421323
asked Jan 2 at 3:48
Tung Vu DucTung Vu Duc
271314
271314
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
You can follow this answer
– TheTiger
Jan 2 at 4:56
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42
add a comment |
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
You can follow this answer
– TheTiger
Jan 2 at 4:56
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
You can follow this answer
– TheTiger
Jan 2 at 4:56
You can follow this answer
– TheTiger
Jan 2 at 4:56
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42
add a comment |
3 Answers
3
active
oldest
votes
You can do this a few ways.
You can attach a gesture and if you tap on a specific part of the frame, then do one thing.
func tapMethod(gesture:UITapGestureRecognizer) {
//on label
let touch = tap.locationInView(button)
If(label.frame.contains(touch)) {
//....
}
//not on label
Else {
/....
}
}
Or you can add 2 tap gestures, one on the label and one on the button, then you can override
func hitTest(_ point: CGPoint,
with event: UIEvent?) -> UIView?
This will allow you to touch on the button’s subviews if necessary as well click on the button’s actions if necessary. Here is a good example. https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b. It reduces the coupling of code and allows for different pieces to come together. This is the hardest route but in my opinion, has the greatest benefit of allowing easiest movement and flow of code
add a comment |
Use tag of label to find which label
titleLabel.tag = 1
captionLabel.tag = 2
then use touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
// to find cell index. use super view of label
if let label = touch.view as! UILabel {
if label.tag == 1 {
// Move to profile screen
} else if label.tag == 2 {
// Move to comments screen
}
}
}
add a comment |
you can assign tags to each of your buttons in cellforRow
Method like cell.button1.tag = 1 ...
and attach a commonEvent to your buttons and detect which button is tapped by sender.tag == 1 { }
and so on ..
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%2f54000993%2fdetect-tap-on-specific-part-of-uibutton%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this a few ways.
You can attach a gesture and if you tap on a specific part of the frame, then do one thing.
func tapMethod(gesture:UITapGestureRecognizer) {
//on label
let touch = tap.locationInView(button)
If(label.frame.contains(touch)) {
//....
}
//not on label
Else {
/....
}
}
Or you can add 2 tap gestures, one on the label and one on the button, then you can override
func hitTest(_ point: CGPoint,
with event: UIEvent?) -> UIView?
This will allow you to touch on the button’s subviews if necessary as well click on the button’s actions if necessary. Here is a good example. https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b. It reduces the coupling of code and allows for different pieces to come together. This is the hardest route but in my opinion, has the greatest benefit of allowing easiest movement and flow of code
add a comment |
You can do this a few ways.
You can attach a gesture and if you tap on a specific part of the frame, then do one thing.
func tapMethod(gesture:UITapGestureRecognizer) {
//on label
let touch = tap.locationInView(button)
If(label.frame.contains(touch)) {
//....
}
//not on label
Else {
/....
}
}
Or you can add 2 tap gestures, one on the label and one on the button, then you can override
func hitTest(_ point: CGPoint,
with event: UIEvent?) -> UIView?
This will allow you to touch on the button’s subviews if necessary as well click on the button’s actions if necessary. Here is a good example. https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b. It reduces the coupling of code and allows for different pieces to come together. This is the hardest route but in my opinion, has the greatest benefit of allowing easiest movement and flow of code
add a comment |
You can do this a few ways.
You can attach a gesture and if you tap on a specific part of the frame, then do one thing.
func tapMethod(gesture:UITapGestureRecognizer) {
//on label
let touch = tap.locationInView(button)
If(label.frame.contains(touch)) {
//....
}
//not on label
Else {
/....
}
}
Or you can add 2 tap gestures, one on the label and one on the button, then you can override
func hitTest(_ point: CGPoint,
with event: UIEvent?) -> UIView?
This will allow you to touch on the button’s subviews if necessary as well click on the button’s actions if necessary. Here is a good example. https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b. It reduces the coupling of code and allows for different pieces to come together. This is the hardest route but in my opinion, has the greatest benefit of allowing easiest movement and flow of code
You can do this a few ways.
You can attach a gesture and if you tap on a specific part of the frame, then do one thing.
func tapMethod(gesture:UITapGestureRecognizer) {
//on label
let touch = tap.locationInView(button)
If(label.frame.contains(touch)) {
//....
}
//not on label
Else {
/....
}
}
Or you can add 2 tap gestures, one on the label and one on the button, then you can override
func hitTest(_ point: CGPoint,
with event: UIEvent?) -> UIView?
This will allow you to touch on the button’s subviews if necessary as well click on the button’s actions if necessary. Here is a good example. https://medium.com/@nguyenminhphuc/how-to-pass-ui-events-through-views-in-ios-c1be9ab1626b. It reduces the coupling of code and allows for different pieces to come together. This is the hardest route but in my opinion, has the greatest benefit of allowing easiest movement and flow of code
answered Jan 2 at 4:52
impression7vximpression7vx
52911037
52911037
add a comment |
add a comment |
Use tag of label to find which label
titleLabel.tag = 1
captionLabel.tag = 2
then use touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
// to find cell index. use super view of label
if let label = touch.view as! UILabel {
if label.tag == 1 {
// Move to profile screen
} else if label.tag == 2 {
// Move to comments screen
}
}
}
add a comment |
Use tag of label to find which label
titleLabel.tag = 1
captionLabel.tag = 2
then use touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
// to find cell index. use super view of label
if let label = touch.view as! UILabel {
if label.tag == 1 {
// Move to profile screen
} else if label.tag == 2 {
// Move to comments screen
}
}
}
add a comment |
Use tag of label to find which label
titleLabel.tag = 1
captionLabel.tag = 2
then use touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
// to find cell index. use super view of label
if let label = touch.view as! UILabel {
if label.tag == 1 {
// Move to profile screen
} else if label.tag == 2 {
// Move to comments screen
}
}
}
Use tag of label to find which label
titleLabel.tag = 1
captionLabel.tag = 2
then use touchesBegan
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
// to find cell index. use super view of label
if let label = touch.view as! UILabel {
if label.tag == 1 {
// Move to profile screen
} else if label.tag == 2 {
// Move to comments screen
}
}
}
edited Jan 2 at 6:10
answered Jan 2 at 4:40
ktr kathirktr kathir
912624
912624
add a comment |
add a comment |
you can assign tags to each of your buttons in cellforRow
Method like cell.button1.tag = 1 ...
and attach a commonEvent to your buttons and detect which button is tapped by sender.tag == 1 { }
and so on ..
add a comment |
you can assign tags to each of your buttons in cellforRow
Method like cell.button1.tag = 1 ...
and attach a commonEvent to your buttons and detect which button is tapped by sender.tag == 1 { }
and so on ..
add a comment |
you can assign tags to each of your buttons in cellforRow
Method like cell.button1.tag = 1 ...
and attach a commonEvent to your buttons and detect which button is tapped by sender.tag == 1 { }
and so on ..
you can assign tags to each of your buttons in cellforRow
Method like cell.button1.tag = 1 ...
and attach a commonEvent to your buttons and detect which button is tapped by sender.tag == 1 { }
and so on ..
edited Jan 2 at 9:35
ktr kathir
912624
912624
answered Jan 2 at 6:07
user3001880user3001880
32
32
add a comment |
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%2f54000993%2fdetect-tap-on-specific-part-of-uibutton%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
stackoverflow.com/questions/1256887/…
– Dixit Akabari
Jan 2 at 4:40
So you want to have one button that causes different IBActions depending on where the button is pressed?
– swiftcoder
Jan 2 at 4:40
samwize.com/2016/03/04/…
– Dixit Akabari
Jan 2 at 4:41
You can follow this answer
– TheTiger
Jan 2 at 4:56
@swiftcoder yes, button or label. I just guess that instagram using a button with differents NSAttributed because when I long press on it, it looks pretty close like when I do on button
– Tung Vu Duc
Jan 2 at 5:42