ImageView与手势识别

6 篇文章 0 订阅

ImageView就像相框,照片需要放在相框里,才能看到。
还可以设置动画,在一个文件夹里,设置名称基本相同,利用循环把相片加到一个可变数组里面。然后把数组赋给Imageview的animationImagesli。就可以显示动画了,还可以设置动画的次数,快慢。

手势识别器

⼿手势识别器有7个⼦子类
1.轻拍⼿手势、
2.平移⼿手势、
3.轻扫⼿手势、
4.缩放⼿手势、
5.旋转⼿手势、
6.⻓长按⼿手势
7.屏幕边界平移⼿手势

识别器的类别

手势识别继承UIGestureRecognizer,继承NSobject

view类可以添加手势识别器(addGestureRecognizer),就可以做成按钮等等。

UITapGestureRecognizer是轻拍⼿手势识别器,能识别轻拍操作
UILongPressGestureRecognizer是⻓长按⼿手势识别器,能识别⻓长按操作。
UIRotationGestureRecognizer是旋转⼿手势识别器,能识别旋转操作。
UIPinchGestureRecognizer是捏合⼿手势识别器,能识别捏合操作。
UIPanGestureRecognizer是平移⼿手势识别器,能识别拖拽操作。
UISwipeGestureRecognizer是轻扫⼿手势识别器,能识别拖拽操作。
UIScreenEdgePanGestureRecognizer是屏幕边缘轻扫识别器,是iOS7中新增的⼿手势。



#import "RootViewController.h"

@interface RootViewController ()

@property(nonatomic,retain)UIImageView *iocn;

@property(nonatomic,retain)UIImageView *animationView;

@end

@implementation RootViewController

- (void)dealloc
{
    [_iocn release];
    [super dealloc];
}




- (void)viewDidLoad {
    [super viewDidLoad];



    _iocn = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];

    _iocn.frame = [UIScreen mainScreen].bounds;

    //如果没有给_icon设置frame,图片的frame默认为图片的大小,位置为(0,0)
    //如果设置了图片的frame,此时图片会变形
    //但是如果把_icon直接给view,而不是添加到子视图上,以上设置无效。

    [self.view addSubview:_iocn];



    [_iocn release];


#pragma mark ------------使用UIImagView播放动画图片

    _animationView = [[UIImageView alloc]init];
    _animationView.frame = CGRectMake(100, 100, 100, 100);






    NSMutableArray *icons = [NSMutableArray array];
    UIImage *image =[[UIImage alloc]init];
    for (int i = 1; i< 4 ; i++) {

         image = [UIImage imageNamed:[NSString stringWithFormat:@"2-%d(被拖移).tiff",i]];


        [icons addObject:image];
    }

    //将动画数组赋给ImageView
    _animationView.animationImages = icons;


    //设置播放时间
    _animationView.animationDuration = 0.3;

    //设置播放次数
//    _animationView.animationRepeatCount = 3;

    //结束播放动画
//    [_animationView stopAnimating];

    //开始播放动画
    [_animationView startAnimating];



    //添加到父视图上
     [self.view addSubview:_animationView];
    [_animationView release];
    [image release];


#pragma mark --------------UIGestureRecognizer(手势识别器)


    //轻拍手势识别器

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer
                                      alloc]initWithTarget:self action:@selector(tap:)];

    [_animationView addGestureRecognizer:tapGR];
    [tapGR release];

    //ImageView默认是关闭的
    _animationView.userInteractionEnabled = YES;


    //长安手势

    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];

    [_iocn addGestureRecognizer:longGR];

    _iocn.userInteractionEnabled = YES;



    //创建平移手势
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPP:)];

    [_animationView addGestureRecognizer:panGR];


    //创建捏合手势识别器

    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchPP:)];

    [_iocn addGestureRecognizer:pinchGR];
    // Do any additional setup after loading the view.
    //创建旋转手势识别器

    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationPP:)];

    [_animationView addGestureRecognizer:rotationGR];


    //清扫手势

    UISwipeGestureRecognizer *swipwGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipwGR.numberOfTouchesRequired = 1;
    [_iocn addGestureRecognizer:swipwGR];
}

//UISwipeGestureRecognizerDirectionRight = 1 << 0,
//UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
//UISwipeGestureRecognizerDirectionUp    = 1 << 2,
//UISwipeGestureRecognizerDirectionDown  = 1 << 3

-(void)swipe:(UISwipeGestureRecognizer *)GR
{
    switch (GR.direction) {
        case UISwipeGestureRecognizerDirectionRight:
        {
            CGRect temp = _iocn.frame;
            temp.origin.x +=temp.size.width/2;
            _iocn.frame =temp;

        }
            break;
        case UISwipeGestureRecognizerDirectionLeft:
        {
            CGRect temp1 = _iocn.frame;
            temp1.origin.x -=temp1.size.width/2;
            _iocn.frame =temp1;
        }
            break;
        case UISwipeGestureRecognizerDirectionUp:
        {
            CGRect temp2 = _iocn.frame;
            temp2.origin.y +=temp2.size.height/2;
            _iocn.frame =temp2;
        }
            break;
        case UISwipeGestureRecognizerDirectionDown:
        {
            CGRect temp3 = _iocn.frame;
            temp3.origin.y -=temp3.size.height/2;
            _iocn.frame =temp3;
        }
            break;
        default:
            break;
    }

}



-(void)rotationPP:(UIRotationGestureRecognizer *)GR{

    //第二参数指的是弧度,不是角度
    GR.view.transform = CGAffineTransformRotate(GR.view.transform, GR.rotation);
    GR.rotation  = 0;
}


-(void)tap:(UITapGestureRecognizer *)ppp{



    NSLog(@"你在摸我");
}

-(void)pinchPP:(UIPinchGestureRecognizer *)pinch{

//    CGFloat temp = pinch.scale;

    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

    //把设为上次的缩放比例为1;
    pinch.scale = 1;
}

-(void)panPP:(UIPanGestureRecognizer *)pan{

    CGPoint temp  = [pan translationInView:pan.view];
    NSLog(@"%@",NSStringFromCGPoint(temp));



    CGPoint tempCenter = pan.view.center;
    temp.x = temp.x +tempCenter.x;
    temp.y = temp.y + tempCenter.y;

    pan.view.center = temp;


    //为了防止偏移量叠加,要把之前的旧的偏移量重置为0
    [pan setTranslation:CGPointMake(0, 0) inView:pan.view];
}

-(void)longTap:(UILongPressGestureRecognizer *)longPP{
    NSLog(@"你在按我");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值