您的位置:首页 > 其它

利用scrollView实现拖动一段距离,显示下方更多详情

2015-07-08 10:09 176 查看
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

#import "ViewController.h"

@interface
ViewController ()
{

float lastContentOffsetY;

UIImageView * tipImageView;

UILabel * tipLabel;
}

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//创建对象,设置代理,容量

UIScrollView * backScrollView = [[UIScrollView
alloc]initWithFrame:CGRectMake(0,
0, SCREEN_WIDTH,
SCREEN_HEIGHT)];
backScrollView.delegate =
self;
backScrollView.contentSize =
CGSizeMake(SCREEN_WIDTH,
SCREEN_HEIGHT*2);
[self.view
addSubview:backScrollView];

//提示的view

UIView * tipView = [[UIView
alloc]initWithFrame:CGRectMake(0,
SCREEN_HEIGHT - 50,
SCREEN_WIDTH, 50)];
tipView.backgroundColor = [UIColor
grayColor];
[backScrollView
addSubview:tipView];

//提示的文字

tipLabel = [[UILabel
alloc]initWithFrame:CGRectMake(0,
0, 200,
30)];

tipLabel.text =
@"向上拖动阅读更多";

tipLabel.center =
CGPointMake(tipView.center.x+100,
25);
[tipView
addSubview:tipLabel];

//提示的箭头

tipImageView = [[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, 50,
50)];

tipImageView.center =
CGPointMake(tipView.center.x-25,
25);

tipImageView.image = [UIImage
imageNamed:@"11.jpg"];
[tipView
addSubview:tipImageView];

}

#pragma mark--------------scrollView代理方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

//获取刚刚开始点击的y偏移量
为了比较是向上还是向下拖动

lastContentOffsetY = scrollView.contentOffset.y;
}

//一直调用这个方法,实时获取偏移量
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

if (lastContentOffsetY <= scrollView.contentOffset.y)
{

if (scrollView.contentOffset.y>50)
{

tipLabel.text =
@"松手查看详情";

[self
makeTheAnimationWithLayer:tipImageView.layer
withBOOl:TRUE];
}else
{

tipLabel.text =
@"向上拖动阅读更多";

[self
makeTheAnimationWithLayer:tipImageView.layer
withBOOl:FALSE];
}
}

else
{

//向下拖拽的情况

if (SCREEN_HEIGHT - scrollView.contentOffset.y>50)
{

[self
makeTheAnimationWithLayer:tipImageView.layer
withBOOl:FALSE];

tipLabel.text =
@"松手查看详情";
}else
{

tipLabel.text =
@"下拉查看上方信息";

[self
makeTheAnimationWithLayer:tipImageView.layer
withBOOl:TRUE];
}
}


}

//结束拖拽后的调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{


if (lastContentOffsetY < scrollView.contentOffset.y)
{

//向上拖拽的情况

if (scrollView.contentOffset.y>50)
{

[UIView
animateWithDuration:0.4
animations:^{
scrollView.contentOffset =
CGPointMake(0,
SCREEN_HEIGHT);
}
completion:^(BOOL finished) {

//完成动画后,将tiplabel更改

tipLabel.text =
@"下拉查看上方信息";
}];
}else
{

[UIView
animateWithDuration:0.2
animations:^{
scrollView.contentOffset =
CGPointMake(0,
0);
}
completion:^(BOOL finished) {
}];
}
}

else
{

//向下拖拽的情况

if (SCREEN_HEIGHT - scrollView.contentOffset.y>50)
{



[UIView
animateWithDuration:0.4
animations:^{
scrollView.contentOffset =
CGPointMake(0,
0);
}
completion:^(BOOL finished) {

tipLabel.text =
@"向上拖动阅读更多";
}];


}else
{

[UIView
animateWithDuration:0.2
animations:^{
scrollView.contentOffset =
CGPointMake(0,
SCREEN_HEIGHT);
}
completion:^(BOOL finished) {

}];
}
}


}

#pragma mark----------做动画

//箭头翻转的动画
-(void)makeTheAnimationWithLayer:(CALayer *)layer withBOOl:(BOOL )rotate
{

CABasicAnimation *anima=[CABasicAnimation
animationWithKeyPath:@"transform"];

//1.1设置动画执行时间
anima.duration=1.0;

//1.2修改属性,执行动画

if (rotate == FALSE)
{

anima.toValue=[NSValue
valueWithCATransform3D:CATransform3DMakeRotation(0,
0, 0,
1)];
}else
{

anima.toValue=[NSValue
valueWithCATransform3D:CATransform3DMakeRotation(M_PI,
0, 0,
1)];
}

//1.3设置动画执行完毕后不删除动画

anima.removedOnCompletion=NO;

//1.4设置保存动画的最新状态

anima.fillMode=kCAFillModeForwards;

//2.添加动画到layer
[layer
addAnimation:anima
forKey:nil];
}
- (void)didReceiveMemoryWarning {

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: