UIScrollView视差效果动画

时间:2023-03-09 07:31:13
UIScrollView视差效果动画

UIScrollView视差效果动画

UIScrollView视差效果动画

效果

UIScrollView视差效果动画

UIScrollView视差效果动画

源码

https://github.com/YouXianMing/Animations

//
// ScrollImageViewController.m
// Animations
//
// Created by YouXianMing on 15/11/24.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "ScrollImageViewController.h"
#import "MoreInfoView.h"
#import "UIView+SetRect.h"
#import "Math.h" static int type = ;
static int viewTag = 0x11; @interface ScrollImageViewController () <UIScrollViewDelegate> @property (nonatomic, strong) NSArray *picturesArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) Math *onceLinearEquation; @end @implementation ScrollImageViewController - (void)viewDidLoad { [super viewDidLoad];
} - (void)setup { [super setup]; MATHPoint pointA;
MATHPoint pointB; // Type.
if (type % == ) { pointA = MATHPointMake(, -);
pointB = MATHPointMake(self.view.width, - ); } else if (type % == ) { pointA = MATHPointMake(, -);
pointB = MATHPointMake(self.view.width, - ); } else if (type % == ) { pointA = MATHPointMake(, -);
pointB = MATHPointMake(self.view.width, + ); } else if (type % == ) { pointA = MATHPointMake(, -);
pointB = MATHPointMake(self.view.width, + );
} self.onceLinearEquation = [Math mathOnceLinearEquationWithPointA:pointA PointB:pointB]; type++; // Init pictures data.
self.picturesArray = @[[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""]]; // Init scrollView.
CGFloat height = self.view.height;
CGFloat width = self.view.width; _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.bounces = NO;
_scrollView.contentSize = CGSizeMake(self.picturesArray.count * width, height);
[self.view addSubview:_scrollView]; // Init moreInfoViews.
for (int i = ; i < self.picturesArray.count; i++) { MoreInfoView *show = [[MoreInfoView alloc] initWithFrame:CGRectMake(i * width, , width, height)];
show.imageView.image = self.picturesArray[i];
show.tag = viewTag + i; [_scrollView addSubview:show];
} [self bringTitleViewToFront];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat X = scrollView.contentOffset.x; for (int i = ; i < self.picturesArray.count; i++) { MoreInfoView *show = [scrollView viewWithTag:viewTag + i];
show.imageView.x = _onceLinearEquation.k * (X - i * self.view.width) + _onceLinearEquation.b;
}
} @end

细节

UIScrollView视差效果动画