UI一揽子计划 5 (Target: Action:  、Protocol   Delegate、 UIImageView 、手势识别器)

本文介绍了iOS开发中的多种实用设计模式,包括target/action模式和代理模式,并详细讲解了UIImageView类及手势识别器的应用,帮助开发者掌握核心编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、target/ action 设计模式

     耦合是衡量⼀一个程序写的好坏的标准之一,
     耦合是衡量模块与模块之间关联程度的指标
     “高内聚,低耦合是⾯面向对象编程的核⼼心思想
@interface TouchView : UIView
{
   
id _target;  // 记录对象被谁点了
   
SEL _action;  // 点击完成对象要执行什么
}
@property (nonatomic, assign) SEL action;
// 声明一个方法,模拟Button的添加点击事件
- (void)addTarget:(id)target action:(SEL)action;
@end

- (void)addTarget:(id)target action:(SEL)action;
{
   
// 进行赋值
   
_target = target;
   
_action = action;
    // 给这个对象发送一个消息(给这个目标发送一个要执行的事件)withobject 是可以携带的参数
}
// 初始化一个对象
// 处理事件
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [
_target performSelector:_action withObject:self];   // 可以是数组、字典、集合
   
NSLog(@"点我了");
}

// 实现点击方法
- (
void)actionTouchView:(id *)touchView
{
   
TouchView *view = (TouchView *)touchView;
    view.
backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.000];
}

二、代理设计模式

     当一个类的某些功能需要被别人来实现,但是既不明确是些什么 功能,又不明确谁来实现这些功能的时候,委托模式就可以派上⽤场。目的是为了降低类之间的耦合性。

*
协议代理的步骤
    1.
创建一个协议(在协议里写方法)
    2.
声明一个代理属性
    3.
遵守协议 实现方法的具体实现
    4.
设置代理
    5.
让代理执行协议里面的方法(调用协议中的方法)
    6.
进行测试
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
// 让代理去干活(实现协议中的方法)
    [
_delegate changeSize:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   
// 随机改变颜色
    [
_delegate changeColor:self];
   
   
}
#import <UIKit/UIKit.h>
@class DelegateView;

创建一个协议
@protocol DelegateViewDelegate <NSObject>


- (
void)changeSize:(DelegateView *)delegateView;
- (
void)changeColor:(DelegateView *)delegateView;

@end
@interface DelegateView : UIView
// 声明一个属性设置一个代理
@property (nonatomic,retain)id <DelegateViewDelegate> delegate;
@end
实现协议里面的方法
- (void)changeSize:(DelegateView *)delegateView
{
   
DelegateView *view = (DelegateView *)[self.view viewWithTag:2];
   
NSLog(@"有了");
    view.
frame = CGRectMake(100, 100, arc4random() % 275, arc4random() %567);
}

- (
void)changeColor:(DelegateView *)delegateView
{
    
DelegateView *view = (DelegateView *)[self.view viewWithTag:1];
    view.
backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.000];
}
三、UIImageView

UIImageViewiOS中用于显示图⽚片的类,iOS中几乎所有看到的 图片,都是由这个类来显示的。
initWithImage:
initWithContentOfFile:

四、手势识别器

手势识别器是对触摸事件做了封装,我们无需⾃己去判断某个手势
是否触发,手势识别器本身起到了识别作用,我们把重心放在识别之
后要做什么操作上面。
手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。


transformview的⼀一个重要属性,它在矩阵层面上改变view的显示状态,能实现view的缩放、旋转、平移等等功能。
- (void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view.
   
UIImageView *imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    imageView.
image = [UIImage imageNamed:@"faye"];
    [
self.view addSubview:imageView];
    imageView.
userInteractionEnabled = YES;
    [imageView
release];
   
// 手势识别器这个类 是一个抽象类
   
// 自己本身不实现什么具体功能
   
// 具体的功能都是由其子类来实现的
   
/**
    
     //
轻拍
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionTap:)];
     tap.numberOfTouchesRequired = 2;  //
几根手指
     tap.numberOfTapsRequired = 1;  //
轻拍几次
     [imageView addGestureRecognizer:tap];  //
手势添加到view上面
     [tap release];
     */

   
/**
     // 长按
     */
   
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionLongPress:)];
    longPress.
minimumPressDuration = 1;   // 大概最少长按多长时间  默认是0.5
    [imageView
addGestureRecognizer:longPress];
    [longPress
release];
   
/**
     * 
     //
旋转
     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(actionRotation:)];
     [imageView addGestureRecognizer:rotation];
     */

   
/**
     * 
捏合
     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(actionPinch:)];
     [imageView addGestureRecognizer:pinch];
     */

   
/**
     * 
平移
     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(actionPan:)];
     [imageView addGestureRecognizer:pan];
     */


   
/**
     * 
清扫
     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(actionSwipe:)];
     swipe.direction = UISwipeGestureRecognizerDirectionLeft;
     [imageView addGestureRecognizer:swipe];
     [swipe release];

     */

   
   
/**
     * 
边缘扫    默认的  你不加 它也有
     */

   
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(actionScreenEdgePan:)];
    screenEdgePan.
edges = UIRectEdgeLeft;
    [imageView
addGestureRecognizer:screenEdgePan];
    [screenEdgePan
release];
    }



// 抽象类里面有一个属性 UIView类型的,把手势添加到哪个视图上面,当前的属性点的参数就是哪个

- (
void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
   
// 只触发一次  刚一扫就触发
   
if (screenEdgePan.state == UIGestureRecognizerStateEnded) {
       
NSLog(@"边缘扫");
       
UIImageView *view1 = (UIImageView *)screenEdgePan.view;
        view1.
image = [UIImage imageNamed:@"faye1.jpg"];
    }
   
   
   
   
}


- (
void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{
   
NSLog(@"左轻扫");
   
UIImageView *view1 = (UIImageView *)swipe.view;
    view1.
image = [UIImage imageNamed:@"faye1.jpg"];
}


- (
void)actionPan:(UIPanGestureRecognizer *)pan
{
   
NSLog(@"平移了");
   
NSLog(@" x = %f, y = %f", [pan translationInView:pan.view].x, [pan translationInView:pan.view].y);
   
CGPoint p = [pan translationInView:pan.view];
    pan.
view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
   
// 重置,让它以为每次都是刚开始触发
    [pan
setTranslation:CGPointMake(0, 0) inView:pan.view];
   
   
}
- (
void)actionPinch:(UIPinchGestureRecognizer *)pinch
{
   
NSLog(@"捏合了");
   
// transform  形变属性  捏合
   
// 第二个参数 按照捏的刻度比例 形变
    pinch.
view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
   
// 重置比例 1
    pinch.
scale = 1;

}

- (
void)actionRotation:(UIRotationGestureRecognizer *)rotation
{
   
NSLog(@"旋转了");
   
NSLog(@"%f", rotation.rotation);
   
   
// transform 形变属性
   
// 第一个属性 传入要创建哪个视图的形变属性
   
// 第二个属性 传入手势的弧度
    rotation.
view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
   
// 需要重置一下弧度
    rotation.rotation = 0;
}
- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress
{
   
   
if (longPress.state == UIGestureRecognizerStateBegan) {
       
NSLog(@"按按按"); // 长按换张图片
       
UIImageView *view1 = (UIImageView *)longPress.view;
        view1.
image = [UIImage imageNamed:@"faye1.jpg"];
    }
   
}

- (void)actionTap:(UITapGestureRecognizer *)tap
{
   
NSLog(@"拍拍拍");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值