Skip to content

Instantly share code, notes, and snippets.

@heyfluke
Created September 28, 2013 13:43
Show Gist options
  • Save heyfluke/6742216 to your computer and use it in GitHub Desktop.
Save heyfluke/6742216 to your computer and use it in GitHub Desktop.
Smoothed drawing in a scrollview.
#import "ScrolledSmoothedBIView.h"
#import "SmoothedBIView.h"
@implementation ScrolledSmoothedBIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
SmoothedBIView *smoothedBIView = [[SmoothedBIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width*2, frame.size.height*2)];
[self addSubview:smoothedBIView];
self.contentSize = smoothedBIView.frame.size;
self.multipleTouchEnabled = NO;
self.delaysContentTouches = NO;
self.scrollEnabled = NO;
self.showsHorizontalScrollIndicator = YES;
self.showsVerticalScrollIndicator = YES;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self addGestureRecognizer:panGestureRecognizer];
UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(20, 50, 100, 40)];
[left setBackgroundColor:[UIColor greenColor]];
[left setTitle:@"left" forState:UIControlStateNormal];
[left addTarget:self action:@selector(leftPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:left];
UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-150, 50, 100, 40)];
[right setBackgroundColor:[UIColor greenColor]];
[right setTitle:@"right" forState:UIControlStateNormal];
[right addTarget:self action:@selector(rightPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:right];
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint velocity = [gestureRecognizer velocityInView:self];
NSLog(@"pan velocity (%.0lf,%.0lf)", velocity.x, velocity.y);
// if (velocity.x < 20 && velocity.y < 20) {
// return;
// }
double newx = self.contentOffset.x - velocity.x;
double newy = self.contentOffset.y - velocity.y;
if (newx < 0) newx = 0;
if (newy < 0) newy = 0;
[self scrollRectToVisible:CGRectMake(newx, newy, self.frame.size.width, self.frame.size.height) animated:YES];
}
- (void)rightPressed:(id)sender
{
[self scrollRectToVisible:CGRectMake(self.contentOffset.x+self.frame.size.width/2.0, self.frame.origin.y, self.frame.size.width, self.frame.size.height) animated:YES];
}
- (void)leftPressed:(id)sender
{
[self scrollRectToVisible:CGRectMake(self.contentOffset.x-self.frame.size.width/2.0, self.frame.origin.y, self.frame.size.width, self.frame.size.height) animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment