我们常常会看到这样的应用,根据手指的滑动对物体不同角度的观看,
下面是我实现的一些代码:
下面是一个简单的UIViewController 有一个imageView属性,用于图片的切换的视图。还有一个locatianX属性,用于开始X坐标的标记。
#import <UIKit/UIKit.h>
#import "ImageView.h"
@interface ViewController : UIViewController {
IBOutlet ImageView * imageView;
double locationX;
}
@property (nonatomic,retain)IBOutlet ImageView * imageView;
@end
下面是ViewContoller的实现类。里面实现了当手触屏后引发的事件所对应的方法:。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"this is Began....");
UITouch * touch=[touches anyObject];
locationX=[touch locationInView:imageView].x;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"this is End.....");
// locationX=0;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"this is Moved....");
double x=[[touches anyObject]locationInView:imageView].x;
if (x<locationX) {
[imageView doNextImage];
}else {
[imageView doPriviousImage];
}
}
下面是ImageView所对应的类:
该类定义了俩个方法:一个是控制视图左旋转,一个是控制视图右旋转的。
@interface ImageView : UIImageView {
}
-(void)doNextImage;
-(void)doPriviousImage;
@end
下面是实现类中两个方法的代码:
-(void)doNextImage{
self.image=[UIImage imageWithContentsOfFile: [ [NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"7_hawana_%i.jpg",index] ofType:nil ] ];
index++;
index%=36;//需要在此说明的是我的实现的项目中只有36张图片,所以此处为36.
}
-(void)doPriviousImage{
self.image=[UIImage imageWithContentsOfFile: [ [NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"7_hawana_%06i.jpg",index] ofType:nil ] ];
index--;
index%=36;
if (index<0) {
index=index+36;
}
}
当然还要在ViewController.xib文件中配置一下他们之间的关系,在这我就不贴图片了,简单说一下吧,添加一个UIImageView到view中去,并将其类改为“ImageView”,然后右键连接File's Owner 和 ImageView 。
下面是我截取的一些实现后的图片: