⼀、target/action设计模式编程
耦合设计模式
耦合是衡量⼀个程序写的好坏的标准之⼀, 是衡量模块与模块之间关联程度的指标。 “⾼内聚,低耦合”是⾯向对象编程的核⼼思想。数组
使⽤target…action实现解耦缓存
touchView.h:dom
@interface TouchView : UIView
@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;
- (instancetype)initWithTarget:(id)target action:(SEL)action;
动画
@endatom
touchView.mspa
- (instancetype)initWithTarget:(id)target action:(SEL)action
{
self = [super init];
if (self) {
self.target = target;
self.action = action;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"clicked");
[self.target performSelector:self.action withObject:self];.net
}设计
RootViewController.h
#import "RootViewController.h"
#import "TouchView.h"
#import "UIColor+RandomColor.h"
@interface RootViewController ()
@property (nonatomic, retain) TouchView *aTouchView;
@end
@implementation RootViewController
- (void)dealloc
{
[_aTouchView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.aTouchView = [[TouchView alloc] initWithTarget:self action:@selector(changeColor:)];
//changeColor:方法的参数为TouchView.m中performSelector: withObject:方法的obj
//[_target performSelector:sel withObject:obj];
//一个Target-Action在一个时间只能作一个事件
self.aTouchView.frame = CGRectMake(100, 100, 200, 200);
self.aTouchView.backgroundColor = [UIColor redColor];
[self.view addSubview:_aTouchView];
[self.aTouchView release];
self.view.backgroundColor = [UIColor blueColor];
}
- (void)changeColor:(TouchView *)view
{
view.backgroundColor = [UIColor randomColor];
}
@end
⼆、代理设计模式
delegate也是⽤来解耦的,它再也不简简单单让⺫标去执⾏⼀个动 做了,⽽是delegate去处理⼀些列事件、就像UITextFieldDelegate⼀ 样,能监测将要开始编辑,已经开始编辑、return按钮点击等等。
经过使用代理设计模式,实现开始点击自定义视图touchView时变换颜色,结束点击时变换位置
TouchViewDelegate.h
#import <Foundation/Foundation.h>
@class TouchView;
//#import "TouchView.h"
@protocol TouchViewDelegate <NSObject>
@optional
- (void)touchViewTouchBegan:(TouchView *)touchView; //作饭
- (void)touchViewTouchMoved:(TouchView *)touchView;
- (void)touchViewTouchEnd:(TouchView *)touchView;
- (void)touchViewTouchCancel:(TouchView *)touchView;
@end
touchView.h
@interface TouchView : UIView
@property (nonatomic, assign) id<TouchViewDelegate> delegate; //保姆
@end
toucheView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event // 饿了
{
#pragma mark -- Delegate --
if ([self.delegate respondsToSelector:@selector(touchViewTouchBegan:)]) {
[self.delegate touchViewTouchBegan:self];
}
if ([self.delegate respondsToSelector:@selector(touchViewTouchEnd:)]) {
[self.delegate touchViewTouchEnd:self];
}
}
RootViewController.m
#import "RootViewController.h"
#import "TouchView.h"
@interface RootViewController ()
{
TouchView *_touchView1;
TouchView *_touchView2;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
_touchView1 = [[TouchView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
_touchView1.backgroundColor = [UIColor blueColor];
#pragma mark -- Delegate --
//设置代理
_touchView1.delegate = self;
[self.view addSubview:_touchView1];
[_touchView1 release];
}
#pragma mark -- Delegate --
- (void)touchViewTouchBegan:(TouchView *)touchView //作饭
{
touchView.backgroundColor = [UIColor colorWithRed:arc4random() %256 / 255.0 green:arc4random() %256 / 255.0 blue:arc4random() %256 / 255.0 alpha:arc4random() %100 / 100.0];;
}
- (void)touchViewTouchEnd:(TouchView *)touchView
{
touchView.center = CGPointMake(50, 50);
}
@end
利用家庭-保姆来理解:
TouchView: 家庭类
ContainerViewController:保姆类
TouchViewDelegate: 协议
3、UIImageView
UIImageView是iOS中⽤于显⽰图⽚的类,iOS中⼏乎全部看到的 图⽚,都是由这个类来显⽰的。
下面用UIImageView分别演示如何添加静态和动态图片
一、UIImageView常⽤属性
image //设置图⽚
animationImages //设置⼀组动态图⽚
animationDuration //设置播放⼀次⼀组动态图⽚的时间
animationRepeatCount //设置重复次数
startAnimating //开始动画
stopAnimating //结束动画
二、UIImageView帧动画相关属性和方法
@property(nonatomic,copy) NSArray *animationImages;
须要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)
@property(nonatomic) NSTimeInterval animationDuration;
帧动画的持续时间
@property(nonatomic) NSInteger animationRepeatCount;
帧动画的执行次数(默认是无限循环)
- (void)startAnimating;
开始执行帧动画
- (void)stopAnimating;
中止执行帧动画
- (BOOL)isAnimating;
是否正在执行帧动画
三、UIImage的2种加载方式
方式一:有缓存(图片所占用的内存会一直停留在程序中)
+ (UIImage*)imageNamed:(NSString*)name;
方式二:无缓存(图片所占用的内存会在一些特定操做后被清除)
+ (UIImage *)imageWithContentsOfFile:(NSString *)path
- (id)initWithContentsOfFile:(NSString *)path;
+ (UIImage*)imageWithContentsOfFile:(NSString *)path
- (id)initWithContentsOfFile:(NSString*)path;
RootViewController.m
#import "RootViewController.h"
#import "UIColor+RandomColor.h"
@interface RootViewController ()
@property (nonatomic,retain) UIImageView *imgView;
@end
@implementation RootViewController
- (void)dealloc
{
[_imgView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark -- UImageView --
self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; PNG格式的图片能够省略扩展名
// 若是设置_imgView.frame,则图片大小为frame大小,发生形变。若不设置,则frame的大小为图片的大小。
// _imgView.frame = CGRectMake(0, 0, 371, 600);
NSLog(@"%.f %.f", _imgView.frame.size.width, _imgView.frame.size.height);
[self.view addSubview:_imgView];
[_imgView release];
#pragma mark -- 使用UIImageView播放动画图片gif --
UIImageView * animationImage = [[UIImageView alloc] init];
animationImage.frame = CGRectMake(100, 350, 150, 150);
NSMutableArray * imagesArr = [NSMutableArray array];
for (int i = 1; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"tu%d.tiff", i]];
[imagesArr addObject:image];
}
animationImage.animationImages = imagesArr;
animationImage.animationDuration = 0.1;
[animationImage startAnimating];
// [animationImage stopAnimating];
[self.view addSubview:animationImage];
[animationImage release];
}
...
@end
4、⼿势识别器
⼿势识别器是对触摸事件作了封装,咱们⽆需⾃⼰去判断某个⼿势 是否触发,⼿势识别器本⾝起到了识别做⽤,咱们把重⼼放在识别之 后要作什么操做上⾯。
⼿势识别器有7个⼦类,⼀旦指定的⼿势被识别,咱们能够执⾏咱们⾃⼰定义好的操做。
轻拍⼿势:UITapGestureRecognizer
平移⼿势:UIPanGestureRecognizer
轻扫⼿势: UISwipeGestureRecognizer
捏合⼿势:UIPinchGestureRecognizer
旋转⼿势:UIRotationGestureRecognizer
⻓按⼿势:UILongPressGestureRecognizer
屏幕边界平移⼿势: UIScreenEdgePanGestureRecognizer(iOS7+)
RootViewController.m
@interface RootViewController ()
@property (nonatomic,retain) UIImageView *imgView;
@property (nonatomic, retain) UIView *tapView;
@end
@implementation RootViewController
- (void)dealloc
{
[_imgView release];
[_tapView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark -- UIGestureRecognizer --
UIView * tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
tapView.backgroundColor = [UIColor grayColor];
self.tapView = tapView;
[self.view addSubview:tapView];
[tapView release];
//轻拍手势
UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
//添加手势
[self.tapView addGestureRecognizer:tapGR];
[tapGR release];
//长按手势
UILongPressGestureRecognizer * longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.tapView addGestureRecognizer:longGR];
[longGR release];
//平移手势
UIPanGestureRecognizer * panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.tapView addGestureRecognizer:panGR];
// [animationImage addGestureRecognizer:panGR];
[panGR release];
//捏合手势
UIPinchGestureRecognizer * pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[self.tapView addGestureRecognizer:pinchGR];
[pinchGR release];
//旋转
UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
aView.backgroundColor = [UIColor brownColor];
[self.view addSubview:aView];
[aView release];
UIRotationGestureRecognizer * rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[aView addGestureRecognizer:rotationGR];
[rotationGR release];
//轻扫
// UISwipeGestureRecognizer
//屏幕边界平移手势
// UIScreenEdgePanGestureRecognizer
}
- (void)tapOnce:(UITapGestureRecognizer *)GR
{
// self.tapView.hidden = YES;
// self.tapView.backgroundColor = [UIColor randomColor];
NSLog(@"");
}
- (void)longPress:(UILongPressGestureRecognizer *)GR
{
GR.view.backgroundColor = [UIColor randomColor];
}
- (void)pan:(UIPanGestureRecognizer *)GR
{
CGPoint translation = [GR translationInView:GR.view];
NSLog(@"%@", NSStringFromCGPoint(translation));
CGPoint center = GR.view.center;
center.x += translation.x;
center.y += translation.y;
GR.view.center = center;
[GR setTranslation:CGPointMake(0, 0) inView:GR.view];
}
- (void)pinch:(UIPinchGestureRecognizer *)GR
{
GR.view.transform = CGAffineTransformScale(GR.view.transform, GR.scale, GR.scale);
GR.scale = 1;
}
- (void)rotation:(UIRotationGestureRecognizer *)GR
{
//第二个参数是弧度
//180度 3.1415926 M_PI
GR.view.transform = CGAffineTransformRotate(GR.view.transform, GR.rotation);
GR.rotation = 0;
}
...
@end
利用transform属性能够修改控件的位移(位置)、缩放、旋转
建立一个transform属性
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty) ;
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
(注意:angle是弧度制,并非角度制)
在某个transform的基础上进行叠加
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);
清空以前设置的transform属性
view.transform = CGAffineTransformIdentity;