码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 图片循环滚动(切片效果)

时间:2015-12-09 19:04:50      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:

               技术分享                        技术分享

#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

 

iOS 图片循环滚动(切片效果)

标签:

原文地址:http://www.cnblogs.com/lantu1989/p/5033555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!