-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.m
85 lines (61 loc) · 2.55 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// ViewController.m
// CCScrollViewAnimation
//
// Created by ZhangCc on 2018/8/10.
// Copyright © 2018年 ZhangCc. All rights reserved.
//
#import "ViewController.h"
#import "CCAnimationView.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define TAG 1000
// 动画偏移量 指rightView相对于leftView的偏移量
#define AnimationOffset 100
@interface ViewController () <UIScrollViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 7, SCREEN_HEIGHT);
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:scrollView];
for (int i = 0; i < 7 ; i++) {
CCAnimationView *animationView = [[CCAnimationView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
animationView.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[scrollView addSubview:animationView];
animationView.tag = TAG + i;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 偏移量x
CGFloat x = scrollView.contentOffset.x;
NSInteger leftIndex = x/SCREEN_WIDTH;
// NSLog(@"%ld",leftIndex);
//这里的left和right是区分拖动中可见的两个视图
CCAnimationView * leftView = [scrollView viewWithTag:(leftIndex + TAG)];
CCAnimationView * rightView = [scrollView viewWithTag:(leftIndex + 1 + TAG)];
// 移动距离
CGFloat moveLength = SCREEN_WIDTH - AnimationOffset;
// leftView横坐标
CGFloat leftViewX = (leftIndex + 1) * SCREEN_WIDTH;
// leftView拖拽距离
CGFloat leftDragLength = x - leftViewX;
// leftView移动百分比
CGFloat leftMovePer = leftDragLength / SCREEN_WIDTH;
leftView.imageViewX = (moveLength + leftMovePer * moveLength);
NSLog(@"leftView.contentX: %.2f",leftView.imageViewX);
// rightView横坐标
CGFloat rightViewX = leftIndex * SCREEN_WIDTH;
// rightView拖拽距离
CGFloat rightDragLength = x - rightViewX;
// rightView移动百分比
CGFloat rightMovePer = rightDragLength / SCREEN_WIDTH;
rightView.imageViewX = - moveLength + rightMovePer * moveLength;
NSLog(@"rightView.contentX: %.2f",rightView.imageViewX);
}
@end