设计模式,手势识别器

来源:互联网 发布:免费网络拍卖平台 编辑:程序博客网 时间:2024/06/14 14:58

一、target action设计模式

“高内聚,低耦合”是面向对象编程的核心思想。
使用target…action实现解耦,需要让目标执行一个动作的地方

二、代理设计模式

当一个类的某些功能需要被别人来实现,但是既不明确是些什么功能,又不明确谁来实现这些功能的时候,委托模式就可以派上用场。
delegate使用场景
控件有一些列时间点,控制器可以实现这个代理方法,以便在适当
的时机做适当的事。

RootViewcontroller.h#import <UIKit/UIKit.h>#import "TouchViewDelegate.h"@interface RootViewController : UIViewController<TouchViewDelegate>@endRootViewcontroller.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 yellowColor];    [self.view addSubview:_touchView1];    [_touchView1 release];//    //此处self是rootviewController对象//    _touchView1.target = self   ;//    //[_touchView1 setTarget:self];//    _touchView1.action = @selector(changeColor:);//        _touchView2 =[[TouchView alloc]initWithFrame:CGRectMake(100, 250, 100, 100)];    _touchView2.backgroundColor = [UIColor cyanColor];    [self.view addSubview:_touchView2];    [_touchView2 release];    _touchView2.target =self;    _touchView2.action =@selector(changePosition);    //设置代理    _touchView1.delegate  =self;}//一个target Action只能在一个时间点执行一个动作- (void)changeColor:(TouchView *)view{    view.backgroundColor =[UIColor greenColor];}- (void)changePosition{    _touchView2.center =_touchView2.superview.center;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark ----实现touchviewdelegate协议中的方法- (void)touchViewTouchBegan:(TouchView *)touchView{    touchView.backgroundColor =[UIColor blueColor];}- (void)touchViewTouchEnded:(TouchView *)touchView{    touchView.center = CGPointMake(50, 50);}@endTouchViewDelegate.h#import <Foundation/Foundation.h>@class TouchView;@protocol TouchViewDelegate <NSObject>//#import "TouchView.h"会造成循环导入,要使用@class 类名@optional//协议中的方法可选实现//开始触摸- (void)touchViewTouchBegan:( TouchView *)touchView;//边移动边触摸- (void)touchViewTouchMoved:(TouchView *)touchView;//结束触摸- (void)touchViewTouchEnded:(TouchView *)touchView;//触摸取消- (void)touchViewTouchCancel:(TouchView *)touchView;@endTouchView.h#import <UIKit/UIKit.h>#import "TouchViewDelegate.h"@interface TouchView : UIView@property(nonatomic,retain)id target;@property(nonatomic,assign)SEL action;//- (void)addTarget:(id)target action:(SEL)action ;@property(nonatomic,assign)id<TouchViewDelegate> delegate;@endTouch.m#import "TouchView.h"@implementation TouchView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    //self.backgroundColor =[UIColor redColor];    //self.center =self.superview.center;    //[_target 执行  _action];   // [_target performSelector:_action withObject:self];    //代理执行协议中的方法   [_delegate touchViewTouchBegan:self];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//改变位置//先去检测touchViewTouchEnded:方法有没有实现,如果实现,再让delegate执行这个方法    if([_delegate respondsToSelector:@selector(touchViewTouchEnded:)]){             [_delegate touchViewTouchEnded:self];    }}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}@endAppdelegate.m#import "AppDelegate.h"#import "RootViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [_window makeKeyAndVisible];    RootViewController * rootVC =[[RootViewController alloc]init];    _window.rootViewController = rootVC  ;    [rootVC release];     [_window release];    return YES;}

三、imageView显示图片

#import "RootViewController.h"@interface RootViewController ()@property(nonatomic,retain)UIImageView * imgView;@end@implementation RootViewController- (void)dealloc{    [_imgView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.#pragma mark -----UIImageView    UIImage  * image =[UIImage imageNamed:@"sh.jpg"];    self.imgView =[[UIImageView alloc]initWithImage:image ];    //如果没有给——_imgView设置  //  frame,_imgView.frame = (0,0,图片的宽,图片的高);  //如果设置了_imgView的frame,图片的大小会根据其frame改变,此时图片会变形    _imgView.frame =[UIScreen mainScreen].bounds  ;    [self.view addSubview:_imgView];    [_imgView release];#pragma mark-----使用UIImageView播放动画片//    UIImageView * animationImageView = [[UIImageView alloc]init ];//    //    animationImageView.frame = CGRectMake(100, 350, 100, 100);//    //    //    NSMutableArray * imagesArray =[NSMutableArray array];//    //    for (int i  = 1; i <= 3 ; i++) {//        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"tututu-%d(被拖移).tiff",i ]];//        //        [imagesArray addObject:image ];//        //    }//    //将要播放的图片数组赋给imageView;//    //    animationImageView.animationImages =imagesArray ;//    //    //设置动画播放时间//    animationImageView.animationDuration = 0.1;//    //    [animationImageView startAnimating];//    //    //设置动画重复次数//    //animationImageView.animationRepeatCount = 3;//    //    [self.view addSubview:animationImageView ];//    //    [animationImageView release];#pragma UIgesture Recognizer(手势识别器)    //轻拍手势识别器    UITapGestureRecognizer * tapGR =    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];    //imageView交互默认是关闭的    _imgView.userInteractionEnabled = YES;    //将手势识别器添加到_imgView上    [_imgView addGestureRecognizer:tapGR ];    [tapGR release];    //长按手势识别器    UILongPressGestureRecognizer * longGR =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];    [_imgView addGestureRecognizer:longGR];    [longGR release];    //3.创建平移手势识别器    UIPanGestureRecognizer * panGR =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];    [_imgView addGestureRecognizer:panGR];    [panGR release];    //4.创建捏合手势识别器    UIPinchGestureRecognizer * pinchGR =[[ UIPinchGestureRecognizer alloc ]initWithTarget:self action:@selector(pinch:)];    [_imgView addGestureRecognizer:pinchGR];    [pinchGR release];    //5.创建旋转手势识别    UIRotationGestureRecognizer * rotationGR =    [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];    [_imgView addGestureRecognizer:rotationGR];    [rotationGR release]; }- (void)rotation:(UIRotationGestureRecognizer *)GR{    //第二个参数指的是弧度不是角度    //180度 3.14 PI    //90度  3.14/2 PI/2    //GR.roation是获取手指旋转的弧度    GR.view.transform =CGAffineTransformRotate(GR.view.transform, GR.rotation );    GR.rotation =0.0;}- (void)pinch:(UIPinchGestureRecognizer *)GR{    GR.view.transform  = CGAffineTransformScale(GR.view.transform, GR.scale, GR.scale);    GR.scale = 1.0;}- (void)pan:(UIPanGestureRecognizer *)GR{  CGPoint translation =  [GR translationInView:GR.view];    NSLog(@"%@",NSStringFromCGPoint(translation));    CGPoint tempCenter = _imgView.center;    tempCenter.x =tempCenter.x + translation.x;    tempCenter.y =tempCenter.y +translation.y;    _imgView.center =tempCenter ;    //为了防止偏移量叠加,要把之前旧的偏移量置为0    [GR setTranslation:CGPointMake(0, 0) inView:GR.view];}- (void)longPress:(UILongPressGestureRecognizer *)GR{    NSLog(@"你正在长按!");}- (void)tap:(UITapGestureRecognizer *)GR{    NSLog(@"您正在轻拍_imgView!");}
0 0
原创粉丝点击