Last active
August 1, 2016 16:22
-
-
Save pruinis/f743b13211d9f4b6d1b2dfe0a4c4b66c to your computer and use it in GitHub Desktop.
UIPicker detect tap / touch on currently selected row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Obj-C | |
// GestureRecognizer | |
UITapGestureRecognizer *tapToSelect = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedToSelectRow:)]; | |
tapToSelect.delegate = self; | |
[self.pickerView addGestureRecognizer:tapToSelect]; | |
#pragma mark - Actions | |
- (IBAction)tappedToSelectRow:(UITapGestureRecognizer *)tapRecognizer | |
{ | |
if (tapRecognizer.state == UIGestureRecognizerStateEnded) { | |
CGFloat rowHeight = [self.pickerView rowSizeForComponent:0].height; | |
CGRect selectedRowFrame = CGRectInset(self.pickerView.bounds, 0.0, (CGRectGetHeight(self.pickerView.frame) - rowHeight) / 2.0 ); | |
BOOL userTappedOnSelectedRow = (CGRectContainsPoint(selectedRowFrame, [tapRecognizer locationInView:self.pickerView])); | |
if (userTappedOnSelectedRow) { | |
// NSInteger selectedRow = [self.pickerView selectedRowInComponent:0]; | |
// [self pickerView:self.pickerView didSelectRow:selectedRow inComponent:0]; | |
// .. your action here .. | |
} | |
} | |
} | |
#pragma mark - UIGestureRecognizerDelegate | |
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | |
{ | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: Swift | |
// GestureRecognizer | |
let tapToSelect = UITapGestureRecognizer(target: self, action: #selector(tappedToSelectRow(_:))) | |
tapToSelect.delegate = self | |
topicsPicker.addGestureRecognizer(tapToSelect) | |
// MARK: Actions | |
func tappedToSelectRow(tapRecognizer: UITapGestureRecognizer) { | |
if tapRecognizer.state == .Ended { | |
let rowHeight = topicsPicker.rowSizeForComponent(0).height | |
let selectedRowFrame = CGRectInset(topicsPicker.bounds, 0, (CGRectGetHeight(topicsPicker.frame) - rowHeight) / 2.0) | |
let userTappedOnSelectedRow = (CGRectContainsPoint(selectedRowFrame, tapRecognizer.locationInView(topicsPicker))) | |
if userTappedOnSelectedRow { | |
// let selectedRow = topicsPicker.selectedRowInComponent(0) | |
// topicsPicker.selectRow(selectedRow, inComponent: 0, animated: false) | |
// .. your action here .. | |
} | |
} | |
} | |
// MARK: UIGestureRecognizerDelegate | |
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UIPicker detect tap / touch on currently selected row
from stackoverflow