标签:
------------- CZTgHeaderView.h -------------
#import <UIKit/UIKit.h>
@interface CZHeaderView : UIView
+ (instancetype)headerView;
@end
------------- CZTgHeaderView.m -------------
#import "CZHeaderView.h"
const int CZImageCount = 5;
@interface CZHeaderView ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;
@end
@implementation CZHeaderView
+ (instancetype)headerView
{
return [[[NSBundle mainBundle] loadNibNamed:@"CZHeaderView" owner:nil options:nil] lastObject];
}
- (void)awakeFromNib
{
CGFloat imageW = self.scrollView.frame.size.width;
CGFloat imageH = self.scrollView.frame.size.height;
CGFloat imageY = 0;
for (int index = 0; index < CZImageCount; index++)
{
CGFloat imageX = index * imageW;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ad_%02d", index]];
[self.scrollView addSubview:imageView];
}
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(imageW * CZImageCount, 0);
self.scrollView.pagingEnabled = YES;
self.pageControl.numberOfPages = CZImageCount;
self.timer = [NSTimer timerWithTimeInterval:2.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)nextImage
{
if (self.pageControl.currentPage == CZImageCount - 1)
{
self.pageControl.currentPage = 0;
} else {
self.pageControl.currentPage++;
}
CGPoint offset = CGPointMake(self.scrollView.frame.size.width * self.pageControl.currentPage, 0);
[self.scrollView setContentOffset:offset animated:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
self.timer = [NSTimer timerWithTimeInterval:2.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.timer invalidate];
self.timer = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.timer) return;
self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width;
}
@end
------------- CZTgFooterView.h -------------
#import <UIKit/UIKit.h>
@class CZTgFooterView;
@protocol CZTgFooterViewDelegate <NSObject>
@optional
- (void)tgFooterViewDidClickLoadBtn:(CZTgFooterView *)tgFooterView;
@end
@interface CZTgFooterView : UIView
@property (nonatomic, weak) id<CZTgFooterViewDelegate> delegate;
+ (instancetype)footerView;
@end
------------- CZTgFooterView.m -------------
#import "CZTgFooterView.h"
@interface CZTgFooterView ()
@property (weak, nonatomic) IBOutlet UIButton *laodBtn;
@property (weak, nonatomic) IBOutlet UIView *laodView;
- (IBAction)loadingBtnClick;
@end
@implementation CZTgFooterView
+ (instancetype)footerView
{
return [[[NSBundle mainBundle] loadNibNamed:@"CZTgFooterView" owner:nil options:nil] lastObject];
}
- (IBAction)loadingBtnClick
{
self.laodBtn.hidden = YES;
self.laodView.hidden = NO;
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if ([self.delegate respondsToSelector:@selector(tgFooterViewDidClickLoadBtn:)]) {
[self.delegate tgFooterViewDidClickLoadBtn:self];
}
self.laodBtn.hidden = NO;
self.laodView.hidden = YES;
});
}
@end
------------- CZTgCell.h -------------
#import <UIKit/UIKit.h>
@class CZTg;
@interface CZTgCell : UITableViewCell
@property (strong, nonatomic) CZTg *tg;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
------------- CZTgCell.m -------------
#import "CZTgCell.h"
#import "CZTg.h"
@interface CZTgCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UILabel *buyCount;
@property (weak, nonatomic) IBOutlet UILabel *priceView;
@end
@implementation CZTgCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString * ID = @"Tg";
CZTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"CZTgCell" owner:nil options:nil] lastObject];
}
return cell;
}
- (void)setTg:(CZTg *)tg
{
_tg = tg;
self.iconView.image = [UIImage imageNamed:tg.icon];
self.titleView.text = tg.title;
self.buyCount.text = [NSString stringWithFormat:@"%@人购买",tg.buyCount];
self.priceView.text = [NSString stringWithFormat:@"¥%@", tg.price];
}
@end
标签:
原文地址:http://www.cnblogs.com/lixiang2015/p/4719372.html