自定义UIImageView的滑动

学习oc语言这么长时间了,对于object-c这么语言我感觉挺有意思的!真的,当你对一件事物感兴趣的时候,你发现原来认真投入到其中,确实学到不少不少东西!!今天天我就说一下如何让UIInageView像scrollView一样可以来回滑动的。是不是感觉可有趣??嘿嘿大笑大笑
步入正题:首先创建一个MyImageView类来继承于UIImageView类继承
@interface MyImageView : UIImageView
接着在.h文件我要干的事
定义属性
CGPoint _beginPoint;//开始坐标
CGPoint _endPoint;//结束坐标
设置代理
@class MyImageView;
@protocol MyImageViewDelegate
@end
@property(nonatomic,assign)id delegate;
定义协议方法
//向左滑动的协议方法
- (void)moveToLife:(MyImageView* )imageView;
//向右滑动的协议方法
- (void)moveToRight:(MyImageView* )imageView;
对了,还有件事,那就是一定要重写初始化方法
- (id)init;
好,OK,我们来到.m文件中
第一步:
//重写初始化方法
- (id)init
{
self = [super init];
if (self) {
//把UIImageView的用户可交互性置为YES。只有UIImageView的用户可交互性默认为:NO
self.userInteractionEnabled = YES;
//设置圆角
self.layer.cornerRadius = 30;
//切割的属性
self.clipsToBounds = YES;
}
return self;
}
第二步:
//需要用到点击开始和点击结束
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
//获取开始点坐标

//获取touch事件
UITouch* touch = [touches anyObject];
//根据touch事件获取点的坐标
_beginPoint = [touch locationInView:self];

}
第三步:
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
//获取结束点坐标

//获取touch事件
UITouch* touch = [touches anyObject];
//根据touch事件获取点的坐标
_endPoint = [touch locationInView:self];

//NSLog(@"_beginPoint = %@  _endPoint = %@",NSStringFromCGPoint(_beginPoint),NSStringFromCGPoint(_endPoint));

//判断是向左滑动了,还是向右滑动了
//注意:[self.delegate respondsToSelector:@selector(moveToLeft:)]这个保护只是可选方法
if ((_beginPoint.x - _endPoint.x) > 50)
{
    //NSLog(@"左滑动");
    if ([self.delegate respondsToSelector:@selector(moveToLife:)]) {
        [self.delegate moveToLife:self];
    }
}else if ((_beginPoint.x - _endPoint.x) < -50)
{
    //NSLog(@"右滑动");
    if ([self.delegate respondsToSelector:@selector(moveToRight:)]) {
        [self.delegate moveToRight:self];
    }
}

}
好.OK 类写好了,我们来实现一下效果吧
首先倒入头文件

import “MyImageView.h”

go 我们来到viewDidLoad里面,创建数组,在数组里放如几张图片,创建ImageView
MyImageView* imageView = [[MyImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 320, 480);
imageView.image = [_array objectAtIndex:_selectIndex];
imageView.delegate = self;
[self.view addSubview:imageView];
接下来重点来了,注意请看:

pragma mark —–MyImageViewDelegate—–

  • (void)moveToLeft:(MyImageView *)imageView
    {
    NSLog(@”左”);
    _selectIndex++;
    if (_selectIndex > _array.count - 1) {
    _selectIndex–;
    //弹警告框
    UIAlertView*lertView=[[UIAlertView alloc]initWithTitle:@”警告” message:@”这是最后一张了!亲” delegate:self cancelButtonTitle:@”关闭” otherButtonTitles:nil, nil];
    [lertView show];
    return;
    }
    imageView.image = [_array objectAtIndex:_selectIndex];

    //添加动画(CATransion)
    CATransition* sition = [CATransition animation];
    // sition.fillMode = kCAFillModeForwards;
    // sition.duration = 2;
    // sition.endProgress = 0.5;
    // sition.removedOnCompletion = YES;
    sition.type = kCATransitionPush;
    sition.subtype = kCATransitionReveal;
    // sition.type = @”rippleEffect”;
    [imageView.layer addAnimation:sition forKey:@”animation”];
    // imageView.image = [_array objectAtIndex:7];
    }

  • (void)moveToRight:(MyImageView *)imageView
    {
    NSLog(@”右”);
    _selectIndex–;
    if (_selectIndex < 0) {
    _selectIndex++;
    //弹警告框
    UIAlertView*lertView=[[UIAlertView alloc]initWithTitle:@”你造吗” message:@”这是最后一张啦!亲” delegate:self cancelButtonTitle:@”关闭” otherButtonTitles:nil, nil];
    [lertView show];
    return;
    }
    imageView.image = [_array objectAtIndex:_selectIndex];
    //添加动画(CATransion)
    CATransition* sition = [CATransition animation];
    sition.type = kCATransitionPush;
    sition.subtype = kCATransitionReveal;
    [imageView.layer addAnimation:sition forKey:@”animation”];
    }
    OK 以上就是我实现UIImageViwe的滑动的全部过程,希望大家可以多多点评!!!!谢谢,如有不对之处,请指出来,我虚心接受!!!!大笑大笑大笑
    请原谅我是个逗比大笑大笑大笑
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值