miracle_of_thinking原创,欢迎转载!原文地址:
http://blog.csdn.net/miracle_of_thinking/article/details/7208400
转载时请在明显处注明,谢谢!
二、定义类
在工程中新建一个继承自UIView的类,名为“UICoverFlowView”。在.h文件中,定义如下的属性和方法:
#import <UIKit/UIKit.h>
@interface UICoverFlowView : UIView {
}
// 左右两侧各显示image的张数,中间显示1张image
@property (nonatomic) int _SIDEPIECES;
// 设定左右两侧image的缩放程度,以及中间image的缩放程度
@property (nonatomic) CGFloat _SIDESCALE;
@property (nonatomic) CGFloat _MIDDLESCALE;
// images array
@property (nonatomic, retain) NSMutableArray *_arrImages;
// image layer array, 帮助移除原有的sublayer
@property (nonatomic, retain) NSMutableArray *_arrLayers;
// templete layer array, 作为模板(主要用于确定每个位置的position、zPosition、transform等),它保存的layer个数比实际的要多(多出最左和最右两个),这样_arrLayers中两端的layer可避免超出边界
@property (nonatomic, retain) NSMutableArray *_arrTempleteLayers;
// 当前正在显示的image的下标
@property (nonatomic) int _curImageIndex;
// 用于显示当前浏览进度的pageControl
@property (nonatomic, retain) UIPageControl *_pageControl;
// 返回一个实例化对象
+ (id)coverFlowViewWithFrame:(CGRect)frame
andImages:(NSArray*)arrImages
sidePieces:(int)SIDEPIECES
sideScale:(CGFloat)SIDESCALE
middleScale:(CGFloat)MIDDLESCALE;
// 获得当前正中间的image的下标
- (int)getMiddleImageIndex;
@end
由于objective-c没有直接提供定义私有方法的功能,因此我们利用category来定义我们的私有方法。我们打开UICoverFlowView.m文件,在#import "UICoverFlowView.h"与@implementationUICoverFlowView之间加入如下代码:
@interface UICoverFlowView (Private)
// 设置模板
- (void)setTempleteLayers;
// 布局images
- (void)arrangeImages;
// 移除指定的sublayer(在delay一定时间后)
- (void)removeLayerAfterSeconds:(id)removeLayer;
// 移除原有的sublayer
- (void)removeSublayers;
// 清空_arrLayers数组
- (void)cleanLayersArray;
// 添加layer及其“倒影”
- (void)showImageAndReflection:(CALayer*)layer;
// 对bounds进行尺寸调整
- (void)scaleBounds:(CALayer*)layer x:(CGFloat)scaleWidth y:(CGFloat)scaleHeight;
// 加入UIPageControl
- (void)addPageControl;
@end
正如前面介绍所说,我们将使用Core Animation来完成大部分特效,这就需要深入到CALayer层,它属于QuartzCore框架。因此,接下来你需要完成两项工作:1、在你的工程中引入“QuartzCore.framework”;2、在UICoverFlowView.m文件中,加入:
#import <QuartzCore/QuartzCore.h>
接下来的工作你因该相当熟悉了,在UICoverFlowView.m中加入:
@synthesize _SIDEPIECES, _SIDESCALE, _MIDDLESCALE;
@synthesize _arrImages, _arrLayers, _arrTempleteLayers;
@synthesize _curImageIndex;
@synthesize _pageControl;
- (void) dealloc
{
[_arrImages release];
[_arrLayers release];
[_arrTempleteLayers release];
[super dealloc];
}
好了,我们的UICoverFlowView类构建完成,接下来我们将实现上面定义的方法。