1. 設計模式:
面向對象的編程核心思想:高內聚 低耦合
使用target action實現解耦
//MyButton.h文件 用UIView模擬一個按鈕
#import <UIKit/UIKit.h>
@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 )
2. 代理模式:
先在某個類中寫協議(或者建一個protocol文件),然後在該類中調用協議的方法(調用的時候會跳到控制器)。。
再然後執行一下三步:
三步: 遵守協議(控制器遵守)---->設置代理(設置代理爲控制器)---->實現方法(在控制器中實現方法)
3. UIImageView:
- (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];
}
4. 手勢處理:
包括:
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的屬性可以平移 旋轉 縮放.....
記得需要清空上一次的記錄