RootView
#import "RootView.h"
@implementation RootView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor redColor];
[self addAllViews];
}
return self;
}
-(void)addAllViews{
//图片
UIImage *image=[UIImage imageNamed:@"5.png"];
UIImageView *imageView=[[UIImageView alloc] initWithImage:image];
imageView.frame=CGRectMake(70, 100, 200, 200);
//开交互
imageView.userInteractionEnabled=YES;
imageView.tag=101;
imageView.backgroundColor=[UIColor orangeColor];
[self addSubview:imageView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
RootViewController
//
// RootViewController.m
// UILesson5(5)
//
// Created by lanou3g on 14-10-31.
// Copyright (c) 2014年 陈维. All rights reserved.
//
#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()
@property(nonatomic,strong)RootView *rv;
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.rv=[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
return self;
}
-(void)loadView{
self.view=self.rv;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImageView *imv= (UIImageView *)[self.rv viewWithTag:101];
//手势(常见六大手势)
//所有手势都在控制器中添加
//轻点
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(g:)];
//设置点击几下
tap.numberOfTapsRequired=1;
//设置几个手指头
tap.numberOfTouchesRequired=2;
// [imv addGestureRecognizer:tap];
//长按
UILongPressGestureRecognizer *lon=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(long:)];
lon.minimumPressDuration=2.0;
// [imv addGestureRecognizer:lon];
//轻扫
UISwipeGestureRecognizer *sw=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(sw:)];
//轻扫手指
sw.numberOfTouchesRequired=1;
//轻扫方向 左右和上下不能同时使用
sw.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionUp;
// sw.direction=UISwipeGestureRecognizerDirectionLeft;
// [imv addGestureRecognizer:sw];
//拖动
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.minimumNumberOfTouches=1;
// [imv addGestureRecognizer:pan];
//捏合
UIPinchGestureRecognizer *pin=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(Pi:)];
pin.scale=0.5;
[imv addGestureRecognizer:pin];
//旋转
UIRotationGestureRecognizer *rot=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rota:)];
rot.rotation=270;
NSLog(@"%f",rot.velocity);
// [imv addGestureRecognizer:rot];
}
-(void)g:(UITapGestureRecognizer *)sender{
NSLog(@"轻点");
}
-(void)sw:(UISwipeGestureRecognizer *)sender{
NSLog(@"轻扫");
}
-(void)long:(UILongPressGestureRecognizer *)sender{
if(sender.state==UIGestureRecognizerStateBegan){
NSLog(@"长按");
}
}
-(void)rota:(UIRotationGestureRecognizer *)sender{
NSLog(@"旋转");
sender.view.transform=CGAffineTransformMakeRotation(sender.rotation);
//NSLog(@"%f",rot.velocity);
}
-(void)pan:(UIPanGestureRecognizer *)sender{
NSLog(@"拖动");
}
-(void)Pi:(UIPinchGestureRecognizer *)sender{
NSLog(@"捏合");
sender.view.transform=CGAffineTransformMakeScale(sender.scale, sender.scale);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end