|
//
// topicCoverFlowCell.m
// ThePaperHD
//
// Created by scar1900 on 15/2/13.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "topicCoverFlowCell.h"
#import "iCarousel.h"
#import "AsyncImageView.h"
#import "UIImage+ImageEffects.h"
#import "UIImage-Extensions.h"
#import "UIImage+wiRoundedRectImage.h"
#import "StyledPageControl.h"
@interface topicCoverItem : UIView<AsyncImageViewDelegate>
@property(nonatomic, strong)listContObjectVO *contentBO;
@property(nonatomic, strong)AsyncImageView *imageView;
@property(nonatomic, strong)UIImageView *blurView;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIImageView *layerView;
@property(nonatomic, strong)UIImageView *borderFrameView;
@property(nonatomic, strong)UIView *videoView;
@property(nonatomic, strong)UIView *imageNumView;
@property(nonatomic, strong)UIImageView *videoImg;
@property(nonatomic, strong)UILabel *videoDurationLabel;
@property(nonatomic, strong)UIImageView *imageSetImg;
@property(nonatomic, strong)UILabel *imgeSetNumLabel;
@end
@implementation topicCoverItem
@synthesize contentBO = _contentBO;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.blurView];
[self addSubview:self.imageView];
[self addSubview:self.layerView];
[self addSubview:self.titleLabel];
[self addSubview:self.borderFrameView];
self.layer.shouldRasterize = YES;
[self.imageView addSubview:self.videoView];
[self.imageView addSubview:self.imageNumView];
}
return self;
}
- (UIView*)videoView {
if (!_videoView) {
_videoView = [[UIView alloc]initWithFrame:CGRectZero];
_videoView.backgroundColor = [UIColor colorWithHexString:@"0x203032"];
_videoView.hidden = YES;
[_videoView addSubview:self.videoImg];
[_videoView addSubview:self.videoDurationLabel];
}
return _videoView;
}
- (UIImageView*)videoImg {
if (!_videoImg) {
_videoImg = [[UIImageView alloc]initWithImage:Image(@"detailPage/videoIcon.png")];
}
return _videoImg;
}
- (UILabel*)videoDurationLabel {
if (!_videoDurationLabel) {
_videoDurationLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_videoDurationLabel.font = appFont(13, NO);
_videoDurationLabel.textColor = [UIColor whiteColor];
_videoDurationLabel.backgroundColor = [UIColor clearColor];
_videoDurationLabel.textAlignment = NSTextAlignmentCenter;
}
return _videoDurationLabel;
}
- (UIView*)imageNumView {
if (!_imageNumView) {
_imageNumView = [[UIView alloc]initWithFrame:CGRectZero];
_imageNumView.backgroundColor = [UIColor clearColor];
_imageNumView.hidden = YES;
[_imageNumView addSubview:self.imageSetImg];
[_imageNumView addSubview:self.imgeSetNumLabel];
}
return _imageNumView;
}
- (UIImageView*)imageSetImg {
if (!_imageSetImg) {
_imageSetImg = [[UIImageView alloc]initWithImage:Image(@"detailPage/imageListPrint.png")];
}
return _imageSetImg;
}
- (UILabel*)imgeSetNumLabel {
if (!_imgeSetNumLabel) {
_imgeSetNumLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_imgeSetNumLabel.font = appFont(13, NO);
_imgeSetNumLabel.textColor = [UIColor whiteColor];
_imgeSetNumLabel.backgroundColor = [UIColor clearColor];
_imgeSetNumLabel.textAlignment = NSTextAlignmentCenter;
}
return _imgeSetNumLabel;
}
- (UIImageView*)borderFrameView {
if (!_borderFrameView) {
_borderFrameView = [[UIImageView alloc]initWithImage: Image(@"frontPage/coverFlowBorderFrame.png")];
_borderFrameView.backgroundColor = [UIColor clearColor];
_borderFrameView.userInteractionEnabled = YES;
}
return _borderFrameView;
}
- (void)setContentBO:(listContObjectVO *)content {
_contentBO = content;
if ([content.pic isMatchedByRegex:@"gif"]) {
UIImage *smallImage = imageWithUIColor([UIColor colorWithHexString:@"0x4c4c4c"]);
smallImage = [smallImage applyLightDarkEffect];
self.blurView.image = smallImage;
}
if (content.watermark) {
if ([content.watermark intValue] == 1) {
self.videoView.hidden = NO;
self.imageNumView.hidden = YES;
self.videoDurationLabel.text = content.duration;
}else if ([content.watermark intValue] == 2){
self.videoView.hidden = YES;
self.imageNumView.hidden = NO;
self.imgeSetNumLabel.text = content.imageNum;
}else {
self.videoView.hidden = YES;
self.imageNumView.hidden = YES;
}
}else {
self.videoView.hidden = YES;
self.imageNumView.hidden = YES;
}
self.imageView.imageUrl = content.pic;
self.titleLabel.text = content.name;
}
- (AsyncImageView*)imageView {
if (!_imageView) {
_imageView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
_imageView.backgroundColor = [UIColor clearColor];
_imageView.delegate = self;
_imageView.isNeedReductionImage = NO;
}
return _imageView;
}
- (UIImageView*)blurView {
if (!_blurView) {
_blurView = [[UIImageView alloc]initWithFrame:CGRectZero];
_blurView.backgroundColor = [UIColor clearColor];
_blurView.clipsToBounds = YES;
_blurView.layer.cornerRadius = 3.0f;
}
return _blurView;
}
- (UILabel*)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_titleLabel.numberOfLines = 0;
}
_titleLabel.font = appFont(25, NO);
return _titleLabel;
}
- (UIImageView*)layerView {
if (!_layerView) {
_layerView = [[UIImageView alloc]initWithFrame:CGRectZero];
_layerView.image = Image(@"frontPage/layerBack.png");
}
return _layerView;
}
- (void)getImageSuccess:(UIImage *)image {
if (image) {
if (isIOS7) {
if (!self.blurView.image) {
UIImage *smallImage = [self getImageFromImage:CGRectMake(0, image.size.height-170/2, image.size.width, 170/2) sourceImage:image];
smallImage = [smallImage applyLightDarkEffect];
self.blurView.image = smallImage;
}
}else {
self.blurView.backgroundColor = [UIColor colorWithHexString:@"0x4c4c4c"];
}
}
}
- (UIImage*)getImageFromImage:(CGRect)myImageRect sourceImage:(UIImage*)sourceImage{
//大图bigImage
//定义myImageRect,截图的区域
// TPLOG(@"use CGContextDrawImage in coverFlow");
CGImageRef imageRef = sourceImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size = myImageRect.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (!context) {
return nil;
}
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
- (void)layoutSubviews {
self.borderFrameView.frame = self.bounds;
self.imageView.frame = CGRectMake(2,
2,
CGRectGetWidth(self.bounds)-4,
CGRectGetHeight(self.bounds)-170/2-1);
self.blurView.frame = CGRectMake(2,
CGRectGetMaxY(self.imageView.frame),
CGRectGetWidth(self.bounds)-4,
170/2-2);
self.layerView.frame = CGRectMake(2,
CGRectGetMaxY(self.imageView.frame)-182,
CGRectGetWidth(self.bounds)-4,
182);
self.titleLabel.frame = CGRectMake(20,
CGRectGetMaxY(self.imageView.frame),
CGRectGetWidth(self.bounds)-40,
CGRectGetHeight(self.bounds)-CGRectGetHeight(self.imageView.frame));
self.videoView.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)-70, CGRectGetHeight(self.imageView.bounds)-25, 70, 25);
self.imageNumView.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)-50, CGRectGetHeight(self.imageView.bounds)-25, 50, 25);
self.videoImg.frame = CGRectMake(5, CGRectGetHeight(self.videoView.frame)/2-29/4, 29/2, 29/2);
self.videoDurationLabel.frame = CGRectMake(CGRectGetMaxX(self.videoImg.frame), 0, 70-5-29/2, 25);
self.imageSetImg.frame = CGRectMake(0, CGRectGetHeight(self.videoView.frame)/2-33/4, 20, 33/2);
self.imgeSetNumLabel.frame = CGRectMake(CGRectGetMaxX(self.imageSetImg.frame), 0, 30, 25);
}
@end
@interface topicCoverFlowCell()<iCarouselDataSource,iCarouselDelegate>
@property(nonatomic, strong)iCarousel *coverFLow;
@property(nonatomic, strong)NSMutableArray *dataList;
@property(nonatomic, strong)StyledPageControl *pageControl; //页码指示器
//@property(nonatomic, strong)NSTimer *timer;
@end
@implementation topicCoverFlowCell
@synthesize nodeBO = _nodeBO;
@synthesize dataList = _dataList;
@synthesize orientation;
//@synthesize timer;
@synthesize delegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.orientation = [[UIApplication sharedApplication] statusBarOrientation];
[self addSubview:self.coverFLow];
[self addSubview:self.pageControl];
// self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(showNextItem:) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
return self;
}
//- (void)showNextItem:(NSTimer*)timer {
// [self.coverFLow scrollToItemAtIndex:self.coverFLow.currentItemIndex+1 animated:YES];
//}
- (void)setNodeBO:(nodeObjectBO *)nodeBO {
if (_nodeBO) {
return;
}
_nodeBO = nodeBO;
NSArray *dicList = nodeBO.contList;
if (dicList.count > 0) {
NSMutableArray *tempList = [NSMutableArray array];
[dicList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
listContObjectVO *listBO = setJsonDicToDataModel(dic, [listContObjectVO class]);
[tempList addObject:listBO];
}];
self.dataList = tempList;
}
}
- (void)setDataList:(NSMutableArray *)dataList {
_dataList = dataList;
if (dataList.count == 1) {
self.coverFLow.autoscroll = 0;
self.coverFLow.scrollEnabled = NO;
}
self.pageControl.numberOfPages = dataList.count;
[self.coverFLow reloadData];
self.coverFLow.currentItemIndex = 0;
self.pageControl.currentPage = 0;
[self layoutSubviews];
}
- (iCarousel*)coverFLow {
if (!_coverFLow) {
_coverFLow = [[iCarousel alloc]initWithFrame:CGRectZero];
_coverFLow.type = iCarouselTypeLinear;
_coverFLow.delegate = self;
_coverFLow.dataSource = self;
_coverFLow.clipsToBounds = YES;
_coverFLow.backgroundColor = [UIColor clearColor];
_coverFLow.autoscroll = -0.25;
}
return _coverFLow;
}
- (StyledPageControl *)pageControl {
if (!_pageControl) {
_pageControl = [[StyledPageControl alloc] initWithFrame:CGRectZero];
// _pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_pageControl.pageControlStyle = PageControlStyleDefault;
_pageControl.userInteractionEnabled = NO;
_pageControl.hidesForSinglePage = YES;
_pageControl.currentPage = self.coverFLow.currentItemIndex;
_pageControl.coreSelectedColor = [UIColor colorWithHexString:BLUECOLOR];
_pageControl.coreNormalColor = [UIColor colorWithHexString:@"0xc5c5c5"];
_pageControl.strokeWidth = 5;
_pageControl.gapWidth = 10;
_pageControl.diameter = 13;
_pageControl.backgroundColor = [UIColor clearColor];
}
return _pageControl;
}
- (void)layoutSubviews {
CGFloat padding = 0;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
padding = 125;
}
self.coverFLow.frame = CGRectMake(padding,
30,
1312/2,
710/2);
self.pageControl.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-50, CGRectGetMaxY(self.coverFLow.frame)+5, 100, 10);
}
- (void)dealloc {
self.coverFLow.delegate = nil;
self.coverFLow.dataSource = nil;
// [self.timer invalidate];
}
#pragma mark - iCarousel dataSource and delegate
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return self.dataList.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
if (view == nil) {
view = [[topicCoverItem alloc]initWithFrame:CGRectMake(0, 0, 930/2, 710/2)];
}
topicCoverItem *item = (topicCoverItem*)view;
listContObjectVO *contentBO = self.dataList[index];
item.contentBO = contentBO;
return view;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
if (index != carousel.currentItemIndex) {
return;
}
// if ([self.delegate respondsToSelector:@selector(didSelectOneFlow:)]) {
// listContObjectVO *contentBO = self.dataList[index];
// [self.delegate didSelectOneFlow:contentBO];
// }
listContObjectVO *contentBO = self.dataList[index];
if ([self.delegate respondsToSelector:@selector(pushToCoverFlowDetail:)]) {
[self.delegate pushToCoverFlowDetail:contentBO];
}
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel {
self.pageControl.currentPage = carousel.currentItemIndex;
}
- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
//customize carousel display
switch (option)
{
case iCarouselOptionWrap:
{
//normally you would hard-code this to YES or NO
return YES;
}
case iCarouselOptionSpacing:
{
//add a bit of spacing between the item views
if (self.orientation == UIInterfaceOrientationPortrait
|| self.orientation == UIInterfaceOrientationPortraitUpsideDown) {
return value*1.1f;
}else {
return value*1.1f;
}
}
case iCarouselOptionFadeMax:
{
return value;
}
// case iCarouselOptionShowBackfaces:
case iCarouselOptionRadius:
{
return value;
}
// case iCarouselOptionAngle:
// case iCarouselOptionArc:
// case iCarouselOptionTilt:
// case iCarouselOptionCount:
// case iCarouselOptionFadeMin:
// case iCarouselOptionFadeMinAlpha:
// case iCarouselOptionFadeRange:
// case iCarouselOptionOffsetMultiplier:
// case iCarouselOptionVisibleItems:
default:
{
return value;
}
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
|