about the touchmoved function
i have five images,they are named 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg,when my finger moves on the screen of ipad(from right to left),the UIImageView will s开发者_开发问答how the images from 1.jpg to 5.jpg,and from 1.jpg to 5.jpg again, loop,until my finger touchEnd,if my finger moves from left to right,the UIImageView will show the images from 5.jpg to 1.jpg,loop also. i use the following code
int currentTag = 1;
NSArray* pages;
-(void)viewDidLoad{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",nil];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
if (currentTag<[pages count]) {
[imgview setImage:[UIImage imageNamed:[pages objectAtIndex:currentTag]]];
[imgview setUserInteractionEnabled:YES];
imgview.tag=currentTag;
currentTag++;
}
}
}
but it can not work. so what is the right way to do?thanks
It seems you are adding guestureRecognizer to superView of UIImageView but you are using this as well [imgview setUserInteractionEnabled:YES];. Comment this line and your code should work. Because when you set [imgview setUserInteractionEnabled:YES]; then all the touches will be handled by the UIImageView and I assume your ImageView's size is equal to view's size.
精彩评论