Skip to content

Instantly share code, notes, and snippets.

@rustle
Created May 4, 2012 05:35
Show Gist options
  • Save rustle/2592333 to your computer and use it in GitHub Desktop.
Save rustle/2592333 to your computer and use it in GitHub Desktop.
@implementation AppDelegate
{
UITextView *_textView;
}
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, self.window.bounds.size.width, 300)];
_textView.backgroundColor = [UIColor lightGrayColor];
_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.window addSubview:_textView];
UIView *swipeStrip = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _textView.bounds.size.width, 40)];
swipeStrip.backgroundColor = [UIColor lightGrayColor];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[swipeStrip addGestureRecognizer:pan];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[swipeStrip addGestureRecognizer:pinch];
_textView.inputAccessoryView = swipeStrip;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)moveRange:(BOOL)rightToLeftSwipe
{
NSRange range = _textView.selectedRange;
if (range.location == 0 && rightToLeftSwipe)
return;
if ((range.location + range.length >= _textView.text.length) && !rightToLeftSwipe)
return;
rightToLeftSwipe ? range.location-- : range.location++;
_textView.selectedRange = range;
}
- (void)pan:(UIPanGestureRecognizer*)gesture
{
CGPoint translation = [gesture translationInView:gesture.view];
[self moveRange:translation.x < 0];
}
- (void)expandSelection:(BOOL)pinchOut
{
NSRange range = _textView.selectedRange;
if (pinchOut)
{
if (range.location > 0)
range.location--;
if (range.location + range.length < _textView.text.length)
range.length++;
}
else
{
if (range.location + range.length >= _textView.text.length)
range.location++;
if (range.length > 0)
range.length--;
}
_textView.selectedRange = range;
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
CGFloat velocity = pinch.velocity;
[self expandSelection:velocity > 0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment