IOS 下拉刷新 仿QQ
自己按照网上的下拉刷新demo自己做了一个,因为感觉网上下载的demo太繁琐了
下面贴上截图以及源代码
本文转自 http://www.999dh.net/article/iphone_ios_art/44.html 转载请注明!
工程下载地址在上面的链接页面里面,可以去下载!
//CMyDragView.h
#import <UIKit/UIKit.h>
#define MY_DRAG_HIGHT 50
@interface CMyDragView : UIView
{
UIActivityIndicatorView * indicator;
UIScrollView * scrView;
UILabel * textLabel;
UIImageView * imageView;
}
@property(retain,nonatomic) UIScrollView * scrView;
@property(retain,nonatomic) IBOutlet UIActivityIndicatorView * indicator;
@property(retain,nonatomic) IBOutlet UILabel * textLabel;
@property(retain,nonatomic) IBOutlet UIImageView * imageView;;
-(void)InitWithScroll:(UIScrollView*)scrollView;
-(void)BeginUpldate;
-(void)Upldating;
@end
CMyDragView.m
//
// CMyDragView.m
// myrefresh_demo
//
// Created by chaoxiao zhuang on 12-12-22.
// Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
//
#import "CMyDragView.h"
@implementation CMyDragView
@synthesize indicator;
@synthesize textLabel;
@synthesize scrView;
@synthesize imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)InitWithScroll:(UIScrollView*)scrollView
{
[scrollView addSubview:self];
self.scrView = scrollView;
[indicator setHidden:YES];
[indicator stopAnimating];
[self.textLabel setText:@"下拉刷新"];
self.imageView.image = [UIImage imageNamed:@"blueArrow.png"];
}
-(void)BeginUpldate
{
[self.textLabel setText:@"放开刷新"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.imageView.transform = CGAffineTransformMakeRotation(3.14);
[UIView commitAnimations];
}
-(void)EndOfUpldate
{
[indicator stopAnimating];
[indicator setHidden:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[self.scrView setContentInset:UIEdgeInsetsMake(-MY_DRAG_HIGHT, 0, 0, 0)];
[imageView setHidden:NO];
self.imageView.transform = CGAffineTransformMakeRotation(0);
[UIView commitAnimations];
}
-(void)Upldating
{
[UIView beginAnimations:nil context:NULL];
[imageView setHidden:YES];
[indicator startAnimating];
[indicator setHidden:NO];
[self.scrView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[UIView commitAnimations];
[self performSelector:@selector(EndOfUpldate) withObject:nil afterDelay:3];
}
@end
需要新建一个view,将其class 设置为CMyDragView,然后将这个view的simulated Metrics里面的 Status Bar 设置为 None
//
// CRViewController.h
// myrefresh_demo
//
// Created by chaoxiao zhuang on 12-12-22.
// Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CMyDragView.h"
@interface CRViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView * scrollView;
CMyDragView * dragView;
BOOL bIsDrag;
}
@property(retain,nonatomic)IBOutlet UIScrollView * scrollView;
@property(retain,nonatomic) CMyDragView * dragView;
@end
//
// CRViewController.m
// myrefresh_demo
//
// Created by chaoxiao zhuang on 12-12-22.
// Copyright (c) 2012年 taizhouxueyuan. All rights reserved.
//
#import "CRViewController.h"
@implementation CRViewController
@synthesize scrollView;
@synthesize dragView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"CMyDragView" owner:self options:nil];
self.dragView = [array objectAtIndex:0];
[scrollView setContentSize:CGSizeMake(320, 480+MY_DRAG_HIGHT+15)];
[dragView InitWithScroll:scrollView];
[scrollView setContentInset:UIEdgeInsetsMake(-MY_DRAG_HIGHT, 0, 0, 0)];
NSLog(@"viewDidLoad");
bIsDrag = NO;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"DidEndDragging");
bIsDrag = NO;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"WillBeginDragging");
bIsDrag = YES;
}
-(void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
NSLog(@"DidScroll y:%f",_scrollView.contentOffset.y);
if( bIsDrag )
{
if( _scrollView.contentOffset.y <= 0 )
{
[self.dragView BeginUpldate];
}
}
else
{
if( scrollView.contentOffset.y <= 0 )
{
[self.dragView Upldating];
}
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
当然,还需要在xib里面放一些控件,scrollView Label以及imageview等,这个具体可以在工程里面查看。