利用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
#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
相关文章推荐
- Python 2.7(3.x)以及numpy、matplotlib和scipy库三种方法实战安装
- NOSQL数据库Redis原理以及初学步骤
- 第一篇:SSH(Struts+Spring+Hibernate)架构
- 大海教你学手游2015CocosLua第一季_00课程介绍
- 想知道WiFi是什么样子的么?
- Servlet--传参和接参
- include,import,@class的区别
- [调试][程序打印]当printf不能用时,使用C++的不定参数来搞定OutputDebugString打印
- 进程与线程的区别联系
- 共同发展一个以上的开发者账户多台电脑 证书 p12 型材 进出口
- SQLite 数据批量操作
- C语言将24小时制转换为12小时制的方法
- Autorelease
- Maven总结(打war包错误)
- UnityMathf中的数学运算
- 使用Gson进行JSON处理
- POJ 1028 Web Navigation
- 欢迎使用CSDN-markdown编辑器
- 黑马程序员————第十九天
- 数据挖掘方向