zoukankan      html  css  js  c++  java
  • iOS 图片循环滚动(切片效果)

                                           

    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    #import "AppDelegate.h"
    #import "RootViewController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        self.window.rootViewController = [[RootViewController alloc] init];
        
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    #import "RootViewController.h"
    #define ImageCount 5
    @interface RootViewController ()
    {
        UIImageView *_imageView;
        int currentIndex;
    }
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // 定义图片控件
        _imageView = [[UIImageView alloc] init];
        _imageView.frame = [UIScreen mainScreen].bounds;
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.image = [UIImage imageNamed:@"0.jpg"];
        _imageView.userInteractionEnabled = YES;
        [self.view addSubview:_imageView];
        // 添加手势
        UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeAction:)];
        leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
        [_imageView addGestureRecognizer:leftSwipe];
        
        UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeAction:)];
        rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
        [_imageView addGestureRecognizer:rightSwipe];
        
        
    }
    #pragma mark --向左滑动浏览下一张图片--
    - (void)leftSwipeAction:(UISwipeGestureRecognizer *)sender{
        [self transitionAnimation:YES];
    }
    
    #pragma mark --向右滑动浏览上一张图片--
    - (void)rightSwipeAction:(UISwipeGestureRecognizer *)sender{
        [self transitionAnimation:NO];
    }
    
    #pragma mark --旋转动画--
    - (void)transitionAnimation:(BOOL)isLeft{
        //创建转场动画对象
        CATransition *transition = [[CATransition alloc] init];
        //设置动画类型,注意对于苹果官方没公开的动画类型只能使用字符串,并没有对应的常量定义
        transition.type = @"cube";
        //设置子类型
        if (isLeft) {
            transition.subtype = kCATransitionFromRight;
        }else{
            transition.subtype = kCATransitionFromLeft;
        }
        //设置动画时常
        transition.duration = 0.8;
        //设置转场后的新视图添加转场动画
        _imageView.image = [self getImageByIndex:isLeft];
        [_imageView.layer addAnimation:transition forKey:@"KCTransitionAnimation"];
    }
    
    #pragma mark --获取相应的图片--
    - (UIImage *)getImageByIndex:(BOOL)isLeft{
        if (isLeft) {
            currentIndex = (currentIndex + 1) % ImageCount;
        }else{
            currentIndex = (currentIndex - 1 + ImageCount) % ImageCount;
        }
        NSString *imageName = [NSString stringWithFormat:@"%i.jpg",currentIndex];
        return [UIImage imageNamed:imageName];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
  • 相关阅读:
    语音转写平台-非实时语音分割
    基于能量或过零率的实时语音分割--学习笔记
    web浏览器录音:web audio api
    python---webRTC~vad静音检测-学习笔记
    python import自己写的.py文件
    许愿墙JQ
    抽奖JQ
    制作时钟
    冒泡排序
    企业面试:作用域和函数提升的面试题
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5033555.html
Copyright © 2011-2022 走看看