プログラムにフリックさせてイベントを受け取る

2012-10-26#ios

- (void)viewDidLoad {
	[super viewDidLoad];

	self.scrollContainer.animationDelegate = self;
}

- (void)scrollViewWillBeginDragging:(ScrollContainer *)scrollContainer {
	NSLog(@"offset: %d", self.scrollContainer.contentOffset.x);
}

- (void)scrollViewDidEndDecelerating:(ScrollContainer *)scrollContainer {
	NSLog(@"offset: %d", self.scrollContainer.contentOffset.x);
}

- (void)segmentDidChange:(id)sender {
	if (![sender isKindOfClass:[UISegmentControl class]]) {
		return;
	}
	UISegmentControl *segmentControl = sender;

	if (segmentControl.selectedSegmentIndex == 0) {
		[self.scrollContainer moveToPreviousContent];
	} else if (segmentControl.selectedSegmentIndex == 1) {
		[self.scrollContainer moveToNextContent];
	}
}
static CGFloat kContentWidth = 320.0f;

- (void)moveToNextContent {
	[UIView beginAnimation:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	[UIView setAnimationDelegate:self.animationDelegate];
	[UIView setAnimationWillStartSelector:@selector(scrollViewWillBeginDragging:)];
	[UIView setAnimationDidStopSelector:@selector(scrollViewDidEndDecelerating:)];

	CGPoint nextContentOffset = CGPointMake(self.contentOffset.x + kContentWidth, 0);
	self.contentOffset = nextContentOffset;

	[UIView commitAnimations];
}

- (void)moveToPreviousContent {
	[UIView beginAnimation:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	[UIView setAnimationDelegate:self.animationDelegate];
	[UIView setAnimationWillStartSelector:@selector(scrollViewWillBeginDragging:)];
	[UIView setAnimationDidStopSelector:@selector(scrollViewDidEndDecelerating:)];

	CGPoint previousContentOffset = CGPointMake(self.contentOffset.x - kContentWidth, 0);
	self.contentOffset = previousContentOffset;

	[UIView commitAnimations];
}