OC代碼
#import"ViewController.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*topView;
@property(weak,nonatomic)IBOutletUIImageView*bottomView;
@property(weak,nonatomic)IBOutletUIView*dragView;
@property(nonatomic,weak)CAGradientLayer*layer;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//先讓上部分View顯示圖片上部分內(nèi)容
// 0~1
_topView.layer.contentsRect=CGRectMake(0,0,1,0.5);
//設(shè)置錨點(diǎn)
_topView.layer.anchorPoint=CGPointMake(0.5,1);
//設(shè)置下部分顯示內(nèi)容
_bottomView.layer.contentsRect=CGRectMake(0,0.5,1,0.5);
_bottomView.layer.anchorPoint=CGPointMake(0.5,0);
//添加拖動(dòng)手勢
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[_dragViewaddGestureRecognizer:pan];
//陰影效果,添加到底部
CAGradientLayer*layer = [CAGradientLayerlayer];
_layer= layer;
layer.frame=_bottomView.bounds;
//設(shè)置漸變顏色
layer.colors=@[(id)[UIColorclearColor].CGColor,(id)[UIColorblackColor].CGColor];
layer.opacity=0;
//? ? //設(shè)置漸變的方向
//
layer.startPoint = CGPointMake(0, 0);
//
layer.endPoint = CGPointMake(1, 0);
//
//? ? //設(shè)置顏色漸變的開始位置
//
layer.locations = @[@0.5];
[_bottomView.layeraddSublayer:layer];
}
//只要手指在拖動(dòng)的View移動(dòng)的時(shí)候就會(huì)調(diào)用
- (void)pan:(UIPanGestureRecognizer*)pan
{
//獲取手勢偏移量
CGPointtransP =? [pantranslationInView:_dragView];
//計(jì)算旋轉(zhuǎn)角度
CGFloatangle = -transP.y/200.0*M_PI;
//增加立體感
CATransform3Dtransform =CATransform3DIdentity;
//
d:人的眼睛和屏幕的垂直距離
CGFloatd =300;
//設(shè)置形變的m34就可以增加立體感,立體感(近大遠(yuǎn)小)
transform.m34= -1/ d;
//秒速形變的旋轉(zhuǎn)的度數(shù)
transform =CATransform3DRotate(transform,angle,1,0,0);
//開始旋轉(zhuǎn)
_topView.layer.transform= transform;
//設(shè)置陰影的透明度
CGFloatopacity = transP.y/200;
_layer.opacity= opacity;
if(pan.state==UIGestureRecognizerStateEnded) {
//還原
// Damping:值越小,彈簧效果越明顯
// options:秒速動(dòng)畫執(zhí)行過程,勻速,快入快出
[UIViewanimateWithDuration:1delay:0usingSpringWithDamping:0.2initialSpringVelocity:1options:UIViewAnimationOptionCurveEaseInOutanimations:^{
_topView.layer.transform=CATransform3DIdentity;
}completion:^(BOOLfinished) {
}];
//彈簧效果
//上部分形變清空
//陰影透明
_layer.opacity=0;
}
}
@end