面向对象的编程核心思想:高内聚 低耦合
使用target action实现解耦
//MyButton.h文件 用UIView模拟一个按钮
#import
@interface MyButton : UIView
{
id _target;
SEL _action;
}
- (void)addMyTarget:(id)target action:(SEL)action;
@end
//MyButton.m文件
#import "MyButton.h"
@implementation MyButton
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_target performSelector:_action];
}
- (void)addMyTarget:(id)target action:(SEL)action {
_target = target; // self
_action = action; // @selector:
}
@end
_target performSelector:_action : _target调用_action方法 (_action 的类型是SEL )
先在某个类中写协议(或者建一个protocol文件),然后在该类中调用协议的方法(调用的时候会跳到控制器)。。
再然后执行一下三步:
三步: 遵守协议(控制器遵守)---->设置代理(设置代理为控制器)---->实现方法(在控制器中实现方法)
- (void)addAllViews {
#pragma ===== 正常添加--显示图片 =====
self.backgroundColor = [UIColor whiteColor];
UIImageView *imV1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 300, 200)];
UIImageView *imV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 300, 200)];
imV.backgroundColor = [UIColor cyanColor];
// 寻找图片的第一种格式:直接使用imageNamed
// 如果图片格式是png的,可以省略后面的.png 直接写图片的名字即可
imV.image = [UIImage imageNamed:@"1.jpeg"];
// 寻找图片的第二种格式:使用imageWithContentsOfFile
NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpeg"];
// 或者是:pathForResource:@"2.jpeg" ofType:nil
imV1.image = [UIImage imageWithContentsOfFile:path];// 然后将路径赋值
#pragma ===== 动态图 =====
UIImage *img1 = [UIImage imageNamed:@"1.tiff"];
UIImage *img2 = [UIImage imageNamed:@"2.tiff"];
UIImage *img3 = [UIImage imageNamed:@"3.tiff"];
UIImage *img4 = [UIImage imageNamed:@"4.tiff"];
UIImage *img5 = [UIImage imageNamed:@"5.tiff"];
UIImage *img6 = [UIImage imageNamed:@"6.tiff"];
UIImage *img7 = [UIImage imageNamed:@"7.tiff"];
// 1. 动图需要的图片: 用animationImages---是个数组
imV.animationImages = @[img1,img2,img3,img4,img5,img6,img7];
// 动图循环次数 :animationRepeatCount
imV.animationRepeatCount = 2563456;
// 每次动图时间 duration
imV.animationDuration = 0.8;
//让动画开始的方法
[imV startAnimating];
// 动画结束
// [imV stopAnimating];
//意思是如果在图片上面加了例如button这样需要相应的东西,就一定要把imageViewde userInteractionEnabled设置为YES;
imV.userInteractionEnabled = YES; // imageView默认的userInteractionEnabled是NO,会阻断响应链
[self addSubview:imV];
[self addSubview:imV1];
}
包括:
1> 轻拍:UITapGestureRecognizer
2> 长按:UILongPressGestureRecognizer
3> 旋转:UIRotationGestureRecognizer
4> 捏合:UIPinchGestureRecognizer
5> 屏幕边缘清扫:UIScreenEdgePanGestureRecognizer
6> 平移:UIPanGestureRecognizer
7> 清扫:UISwipeGestureRecognizer
- (void)addAllViews {
self.backgroundColor = [UIColor whiteColor];
self.img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpeg"]];
self.img.frame = [UIScreen mainScreen].bounds;
[self addSubview:self.img];
#pragma mark=============手势:===========
#pragma----- 1. 轻拍:
/*
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[self.img addGestureRecognizer:tap];
// 切记:切记:!!! 要把这个userInteractionEnabled打开
// 因为imageView会阻断事件,所以在添加手势时,要把这个userInteractionEnabled打开(打开交互)
self.img.userInteractionEnabled = YES;
// 点击次数
tap.numberOfTapsRequired = 2;
// // 同时有几个触摸点(几个指头触摸)
// tap.numberOfTouchesRequired = 2;
*/
#pragma----- 2. 长按:
/*
UILongPressGestureRecognizer *lo = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:lo];
*/
#pragma----- 3. 旋转:
/*
UIRotationGestureRecognizer *ro = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:ro];
*/
#pragma----- 4. 捏合
/*
UIPinchGestureRecognizer *pin= [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:pin];
*/
#pragma----- 5. 屏幕边缘清扫
/*
UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction)];
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:edge];
*/
#pragma----- 6. 平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:pan];
#pragma----- 7. 清扫手势
/*
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.direction = UISwipeGestureRecognizerDirectionRight;
self.img.userInteractionEnabled = YES;
[self.img addGestureRecognizer:swipe];
*/
}
- (void)tapAction {
NSLog(@"轻拍~ ~ ~");
}
- (void)longAction:(UILongPressGestureRecognizer *)longG {
if (longG.state == UIGestureRecognizerStateBegan) {
// 如果不添加这个判断条件 会打印两次结果:一次是开始 一次是结束
NSLog(@"长按~ ~ ~");
}
}
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
self.img.transform = CGAffineTransformRotate(self.img.transform, sender.rotation);
// 把之前一次记录旋转的角度清空
[sender setRotation:0];// 清空
NSLog(@"旋转 ....");
}
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
self.img.transform = CGAffineTransformScale(self.img.transform, sender.scale, sender.scale);
[sender setScale:1]; // 清空
NSLog(@"pinch.....");
}
- (void)edgeAction {
NSLog(@"edge....");
}
- (void)panAction:(UIPanGestureRecognizer *)sender {
NSLog(@"pan....");
CGPoint point = [sender translationInView:self.img];// 获取点的新信息
self.img.transform = CGAffineTransformTranslate(self.img.transform, point.x, point.y );
// 把每次保存的之前一次的移动距离清空
[sender setTranslation:CGPointZero inView:self.img];
// 每次都会回到原点再进行平移
// self.img.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}
- (void)swipeAction {
NSLog(@"swipe....");
}
View的transform的属性可以平移 旋转 缩放.....
记得需要清空上一次的记录