自定义UIImageView的滑动

来源:互联网 发布:mysql 数据库快照 编辑:程序博客网 时间:2024/06/05 02:59

学习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
原创粉丝点击