zoukankan      html  css  js  c++  java
  • iPhone开发之UIImageView左右划动切换图片

    上下划动可以用类似的方法实现:

    #import <UIKit/UIKit.h>
    
    @interface Abstruct : UIViewController <UIScrollViewDelegate>{
        UIImageView* myimageview;
        NSMutableArray *imgArray;
        CGPoint FirstPoint;
        CGPoint SecondPoint;
        NSInteger Page;
        BOOL touchMove;
    }
    @property (retain, nonatomic)  UIImageView* myimageview;
    @property (retain, nonatomic)  NSMutableArray *imgArray;
    @property NSInteger Page;
    
    @end
    - (void)viewDidLoad
    {
        
        [super viewDidLoad];
    
    imgArray = [[NSMutableArray alloc] initWithObjects:
                    [UIImage imageNamed:@"01-01-1.jpg"],
                    [UIImage imageNamed:@"01-01-2.jpg"],
                    [UIImage imageNamed:@"01-01-3.jpg"],
                    [UIImage imageNamed:@"01-01-4.jpg"],
                    [UIImage imageNamed:@"01-01-5.jpg"],
                    [UIImage imageNamed:@"01-01-6.jpg"],
                    [UIImage imageNamed:@"01-01-7.jpg"],nil];
     self.navigationItem.title = [NSString stringWithFormat:@"1/%d",[imgArray count]];
         self.myimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44,768, 1004)];
        [self.myimageview setImage:[imgArray objectAtIndex:Page]];
        [self.view addSubview:myimageview];
    
    }
    
    
    /*=======================================================
     //触摸事件:点击弹出导航条,左右划动切换经文
     ========================================================*/
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint point = [touch locationInView:self.view];
        FirstPoint = point;
        touchMove = NO;
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        SecondPoint = point;
        touchMove = YES;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        
        if(touchMove == NO)
        {
        }else 
        {
            if (FirstPoint.x > SecondPoint.x)
            {
                if(Page < [imgArray count] - 1)
                {
                    Page++;
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [UIView beginAnimations:nil context:context];
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                    [myimageview setAlpha:0.0];
                    [UIView setAnimationDuration:0.05];
                    [UIView commitAnimations];
                    [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1];
                }else if (Page == [imgArray count] -1) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [UIView beginAnimations:nil context:context];
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                    [myimageview setAlpha:0.0];
                    [UIView setAnimationDuration:0.05];
                    [UIView commitAnimations];
                    Page = 0;
                    [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1];
                }
            }else {
                if(Page >= 1)
                {
                    Page--;
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [UIView beginAnimations:nil context:context];
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                    [myimageview setAlpha:0.0];
                    [UIView setAnimationDuration:0.05];
                    [UIView commitAnimations];
                    [self performSelector:@selector(ChangeImage) withObject:nil afterDelay:0.1];
                }
            }    
        }    
    }
    
    
    /*=======================================================
     更新图片显示
     ========================================================*/
    -(void)ChangeImage{
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.5]; //动画长度,单位为秒
        [self.view setAlpha:1];
        [myimageview setAlpha:1];
        [myimageview setImage:[imgArray objectAtIndex:Page]];
        //[myimageview setAnimationRepeatCount:4]; //设置重复次数
        [UIView commitAnimations];
        self.navigationItem.title = [NSString stringWithFormat:@"%d/%d",Page+1,[imgArray count]];
    }
    
    
    @end
  • 相关阅读:
    input 标签取消readOnly属性
    python selenium 跑脚本的时候按钮无法点击的解决办法
    Python Selenium 调用IE浏览器失败Unexpected error launching Internet Explorer解决方法
    转载--Python random模块(获取随机数)常用方法和使用例子
    转载--python selenium实现文件、图片上传
    ieDriver启动IE浏览器显示This is the initial start page for the WebDriver server的解决办法
    自动化测试用例设计学习心得总结
    关于selene安装插件ide不能识别插件的问题解决办法
    cmd 启动mysql
    最大子序列
  • 原文地址:https://www.cnblogs.com/foxmin/p/2445612.html
Copyright © 2011-2022 走看看