こんにちは、TimeLine という名前の ScrollView を使用したプロジェクトがあります
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger days = 365;
NSInteger lastPos = 0;
// timeline.minimumZoomScale=0.1;
// timeline.maximumZoomScale=1.1;
cash = [NSMutableArray array];
for (int i = 0; i < days; i++) {
int coll = (arc4random() % 400) ;
UILabel *LABBB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
timelineImage = [[UIImageView alloc] init];
timelineImage.frame = CGRectMake(lastPos, coll, 60, 768 - coll);
LABBB.textAlignment = NSTextAlignmentCenter;
LABBB.textColor = [UIColor blackColor];
LABBB.backgroundColor = [UIColor clearColor];
LABBB.font = [UIFont systemFontOfSize:20.f];
LABBB.text = [NSString stringWithFormat:@"%d", 768 - coll];
[cash addObject:[NSNumber numberWithInt:768 - coll]];
[timelineImage addSubview:LABBB];
LABBB.textColor = [UIColor blackColor];
timelineImage.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:102.0/255.0 blue:0.0/255.0 alpha:0.8
];
[timeline addSubview:timelineImage];
lastPos += 61;
}
pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)];
[timeline addGestureRecognizer:pinchGestureRecognizer];
timeline.userInteractionEnabled = YES;
timeline.delegate = self;
timeline.contentSize = CGSizeMake(61 * days, 748);
timeline.pagingEnabled = YES;
NSLog(@"Start %f",timeline.contentSize.width);
// Do any additional setup after loading the view, typically from a nib.
}
ユーザーがズームを終了した後、ScrollViewのコンテンツを365日から37年に変更する必要があります
-(void)handlePinches:(UIPinchGestureRecognizer*)paramSender {
if (paramSender.state == UIGestureRecognizerStateEnded) {
currentScale = paramSender.scale;
if (paramSender.scale < 1.0f ) {
[UIView animateWithDuration:1.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
timeline.pagingEnabled = NO;
timeline.transform = CGAffineTransformMakeScale(0.10, 1.0 );
CGRect scrollFrame;
scrollFrame.origin = CGPointMake(0, 0);
scrollFrame.size = CGSizeMake( 1024, timeline.frame.size.height);
timeline.frame = scrollFrame;
timeline.contentSize = CGSizeMake( (61 * 365) , 748);
//CGSize newZoomSize = CGSizeMake((61 * 365), 748);
// get the center point
CGPoint center = [timeline contentOffset];
center.x += 0;
center.y += 0;
// since pinch zoom changes the contentSize of the scroll view, translate this point to
// the "target" size (from the current size)
//center = [self translatePoint:center currentSize:[timeline contentSize] newSize:newZoomSize];
[timeline setContentOffset:center animated:NO];
}completion:^(BOOL finished){
if (finished) {
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:3];
timeline.alpha = 0.2;
[UIView commitAnimations];
}completion:nil];
[self resetZoom];
[self zoomOutDecade];
}
}];
} else {
NSLog(@"Zoom in");
}
NSLog(@"End scale %f",timeline.contentSize.width);
} else if (paramSender.state == UIGestureRecognizerStateBegan && currentScale != 0.0f)
{
paramSender.scale = currentScale;
} else if(paramSender.scale != NAN && paramSender.scale != 0.0 && paramSender.scale >= 0.1) {
timeline.transform = CGAffineTransformMakeScale(paramSender.scale, 1.0 );
CGRect scrollFrame;
scrollFrame.origin = CGPointMake(0, 0);
scrollFrame.size = CGSizeMake( 1024, timeline.frame.size.height);
timeline.frame = scrollFrame;
timeline.contentSize = CGSizeMake( (61 * 365) , 748);
//NSLog(@"%.3f" ,paramSender.scale);
}
}
しかし、変化はスムーズでなければなりません。
-(void)zoomOutDecade で ScrollView を変更する:
-(void)zoomOutDecade {
NSInteger days = 365;
NSInteger lastPos = 0;
NSInteger decades = days / 10;
if ((days % 10 ) > 0) {
decades ++;
}
timeline.userInteractionEnabled = YES;
timeline.delegate = self;
timeline.contentSize = CGSizeMake(61 * decades, 748);
timeline.pagingEnabled = YES;
// timeline.minimumZoomScale=0.1;
// timeline.maximumZoomScale=1;
[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:3];
[[timeline subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[UIView commitAnimations];
// Do any additional setup after loading the view, typically from a nib.
for (int i = 0; i < decades; i++) {
int coll = (arc4random() % 400) ;
UILabel *LABBB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
timelineImage = [[UIImageView alloc] init];
timelineImage.frame = CGRectMake(lastPos, coll, 60, 768 - coll);
LABBB.textAlignment = NSTextAlignmentCenter;
LABBB.textColor = [UIColor blackColor];
LABBB.backgroundColor = [UIColor clearColor];
LABBB.font = [UIFont systemFontOfSize:20.f];
int summ = 0;
int first = i * 10;
for (int j = first; j < first + 10 && j < days; j++ ) {
summ += [[cash objectAtIndex:j] intValue];
}
LABBB.text = [NSString stringWithFormat:@"%d", summ];
[timelineImage addSubview:LABBB];
LABBB.textColor = [UIColor blackColor];
timelineImage.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:102.0/255.0 blue:0.0/255.0 alpha:0.8
];
[timeline addSubview:timelineImage];
lastPos += 61;
}
[UIView beginAnimations:@"fade" context:nil];
[UIView setAnimationDuration:3];
timeline.alpha = 1.0;
[UIView commitAnimations];
NSLog(@"End scale %f",timeline.contentSize.width);
}