Created
October 30, 2014 02:44
-
-
Save Sunnyztj/683b3da3d0e17b9664dc to your computer and use it in GitHub Desktop.
add tap to label and toggle url event
This file contains 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
- (void)addTagGestureRecognizerToURLLabel { | |
// first image tag gesture recognizer setup | |
UITapGestureRecognizer *firstURLSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(url1TapEvent)]; | |
firstURLSingleTap.numberOfTapsRequired = 1; | |
self.link1Url.userInteractionEnabled = YES; | |
[self.link1Url addGestureRecognizer:firstURLSingleTap]; | |
UITapGestureRecognizer *secondURLSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(url2TapEvent)]; | |
secondURLSingleTap.numberOfTapsRequired = 1; | |
self.link2Url.userInteractionEnabled = YES; | |
[self.link2Url addGestureRecognizer:secondURLSingleTap]; | |
} | |
- (void)url1TapEvent { | |
[self toggleLinkToURL:self.link1Url.text]; | |
} | |
- (void)url2TapEvent { | |
[self toggleLinkToURL:self.link2Url.text]; | |
} | |
- (void)toggleLinkToURL:(NSString *)url { | |
NSString *urlString = nil; | |
if (url != nil && ![url isEqualToString:@""]) { | |
urlString = [urlString hasPrefix:@"http"] ? url : [NSString stringWithFormat:@"http://%@", url]; | |
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) { | |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment