|
//
// coverFlowView.m
// ThePaperHD
//
// Created by scar1900 on 15/1/4.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "coverFlowView.h"
#import "iCarousel.h"
#import "AsyncImageView.h"
#import "UIImage+ImageEffects.h"
#import "UIImage-Extensions.h"
#import "UIImage+wiRoundedRectImage.h"
@interface coverflowItem : UIView<AsyncImageViewDelegate> {
}
@property(nonatomic, strong)listContObjectVO *contentBO;
@property(nonatomic, strong)AsyncImageView *imageView;
@property(nonatomic, strong)UIImageView *blurView;
@property(nonatomic, strong)UILabel *parentNodeLabel;
@property(nonatomic, strong)UILabel *assistantLabel;
@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 coverflowItem
@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.parentNodeLabel];
[self addSubview:self.assistantLabel];
[self addSubview:self.titleLabel];
[self addSubview:self.borderFrameView];
self.layer.shouldRasterize = YES;
[self addSubview:self.videoView];
[self addSubview:self.imageNumView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAfterOpenNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
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;
if ([[TPUserDefault instance].isNightMode intValue] == 0) {
_borderFrameView.image = Image(@"frontPage/coverFlowBorderFrame.png");
}else {
_borderFrameView.image = Image(@"frontPage/coverFlowBorderFrame_night.png");
}
}
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;
NSMutableArray *channalList = [TPUserDefault instance].channldataList;
[channalList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
NSString *currentNodeId =content.parentNodeId;
if ([currentNodeId isEqualToString:dic[@"nodeId"]]) {
if ([dic[@"name"] isEqualToString:@"澎湃"]) {
self.parentNodeLabel.text = @"精选";
self.assistantLabel.text = dic[@"enname"];
}else {
self.parentNodeLabel.text = dic[@"name"];
self.assistantLabel.text = dic[@"enname"];
}
}
}];
}
- (AsyncImageView*)imageView {
if (!_imageView) {
_imageView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
_imageView.backgroundColor = [UIColor clearColor];
// _imageView.layer.cornerRadius = 3.0f;
// _imageView.clipsToBounds = YES;
_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*)parentNodeLabel {
if (!_parentNodeLabel) {
_parentNodeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_parentNodeLabel.backgroundColor = [UIColor clearColor];
_parentNodeLabel.textColor = [UIColor whiteColor];
_parentNodeLabel.textAlignment = NSTextAlignmentCenter;
}
_parentNodeLabel.font = appFont(18, NO);
return _parentNodeLabel;
}
- (UILabel*)assistantLabel {
if (!_assistantLabel) {
_assistantLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_assistantLabel.backgroundColor = [UIColor clearColor];
_assistantLabel.textColor = [UIColor whiteColor];
_assistantLabel.textAlignment = NSTextAlignmentCenter;
}
_assistantLabel.font = appFont(9, NO);
return _assistantLabel;
}
- (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 = getImageFromImage(CGRectMake(0, image.size.height-170/2, image.size.width, 170/2), image);
// smallImage = [smallImage imageRotatedByDegrees:180];
smallImage = [smallImage applyLightDarkEffect];
// smallImage = [UIImage createRoundedRectImage:smallImage size:smallImage.size radius:6];
self.blurView.image = smallImage;
}
}else {
self.blurView.backgroundColor = [UIColor colorWithHexString:@"0x4c4c4c"];
}
}
}
UIImage* getImageFromImage(CGRect myImageRect,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 {
[super 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.parentNodeLabel.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-20,
CGRectGetHeight(self.bounds)-245/2, 50, 45/2);
self.assistantLabel.frame = CGRectMake(CGRectGetMinX(self.parentNodeLabel.frame),
CGRectGetMaxY(self.parentNodeLabel.frame), 50, 25/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)-69, CGRectGetHeight(self.imageView.bounds)-23, 70, 25); //视频图片位置(bug:3877)
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);
}
- (void)refreshAfterOpenNightMode:(NSNotification*)noti {
if ([[TPUserDefault instance].isNightMode intValue] == 0) {
self.borderFrameView.image = Image(@"frontPage/coverFlowBorderFrame.png");
}else {
self.borderFrameView.image = Image(@"frontPage/coverFlowBorderFrame_night.png");
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
@interface coverFlowView()<iCarouselDataSource,iCarouselDelegate> {
}
@property (nonatomic,strong)NSMutableArray *originList;
@property (nonatomic, strong)iCarousel *carousel;
@end
@implementation coverFlowView
@synthesize dataList = _dataList;
@synthesize orientation = _orientation;
@synthesize delegate;
@synthesize originList;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.orientation = [[UIApplication sharedApplication] statusBarOrientation];
self.dataList = [NSArray array];
[self addSubview:self.carousel];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (iCarousel*)carousel {
if (!_carousel) {
_carousel = [[iCarousel alloc]initWithFrame:CGRectZero];
_carousel.type = iCarouselTypeCoverFlow;
_carousel.delegate = self;
_carousel.dataSource = self;
_carousel.backgroundColor = [UIColor clearColor];
_carousel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
}
return _carousel;
}
- (void)setOrientation:(UIInterfaceOrientation)orient {
_orientation = orient;
}
- (void)setDataList:(NSArray *)list {
if (list.count >0) {
NSMutableArray *channelList = [TPUserDefault instance].channldataList;
NSMutableArray *tempArray = [NSMutableArray array];
[channelList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
NSString *nodeId = dic[@"nodeId"];
[list enumerateObjectsUsingBlock:^(listContObjectVO *listObj, NSUInteger index, BOOL *stoped) {
if ([listObj.parentNodeId isEqualToString:nodeId]) {
[tempArray addObject:listObj];
}
}];
}];
// self.originList = [NSMutableArray arrayWithArray:tempArray];
// if (tempArray.count == 4) {
// NSMutableArray *addArray = [NSMutableArray array]; //当个数为4时复制一个作为数据源凑齐5个数据
// [tempArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// [addArray addObject:obj];
// if (idx == 2) {
// [addArray addObject:obj];
// }
// }];
// tempArray = addArray;
// }
_dataList = [NSArray arrayWithArray:tempArray];
}else {
_dataList = list;
}
[self.carousel reloadData];
self.carousel.currentItemIndex = 0;
}
#pragma mark - iCarousel delegate and datasource
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return self.dataList.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
if (view == nil) {
view = [[coverflowItem alloc]initWithFrame:CGRectMake(0, 0, 938/2, 710/2)];
}
coverflowItem *item = (coverflowItem*)view;
listContObjectVO *contentBO = self.dataList[index];
item.contentBO = contentBO;
return view;
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel {
// if (self.originList.count == 4) {
// NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataList];
//
// }
}
- (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];
}
}
- (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 * 0.625f;
}else return value*1.175f;
}
case iCarouselOptionFadeMax:
{
return value;
}
// case iCarouselOptionShowBackfaces:
// case iCarouselOptionRadius:
// case iCarouselOptionAngle:
// case iCarouselOptionArc:
// case iCarouselOptionTilt:
// case iCarouselOptionCount:
// case iCarouselOptionFadeMin:
// case iCarouselOptionFadeMinAlpha:
// case iCarouselOptionFadeRange:
// case iCarouselOptionOffsetMultiplier:
// case iCarouselOptionVisibleItems:
default:
{
return value;
}
}
}
- (void)layoutSubviews {
[super layoutSubviews];
self.carousel.frame = CGRectMake(0,
8,
CGRectGetWidth(self.bounds),
710/2);
}
- (void)dealloc {
self.carousel.delegate = nil;
self.carousel.dataSource = nil;
}
@end
|