#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 + ) % ImageCount;
}else{
currentIndex = (currentIndex - + 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