iOS刷卡function

美好的一天! 我有一个问题,我有一个单页应用程序的代码

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint endPosition = [touch locationInView:self.view]; if (self.imageStatus>0) { if (self.startPosition.x < endPosition.x) { // Right swipe CATransition *transition = [CATransition animation]; transition.duration = 0.75; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; transition.type = kCATransitionPush; transition.subtype =kCATransitionFromLeft; transition.delegate = self; [self.view.layer addAnimation:transition forKey:nil]; [self.view addSubview:myViewController.view]; self.imageStatus --; if (self.imageStatus == 0) { self.myImage.image = [UIImage imageNamed:@"blue_back.png"]; } else { self.myImage.image = [UIImage imageNamed:@"black_back.png"]; } } } if (self.imageStatus<2) { if (self.startPosition.x > endPosition.x) { // Left swipe CATransition *transition = [CATransition animation]; transition.duration = 0.75; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; transition.type = kCATransitionPush; transition.subtype =kCATransitionFromRight; transition.delegate = self; [self.view.layer addAnimation:transition forKey:nil]; [self.view addSubview:myViewController.view]; self.imageStatus ++; if (self.imageStatus == 2) { self.myImage.image = [UIImage imageNamed:@"red_back.png"]; } else { self.myImage.image = [UIImage imageNamed:@"black_back.png"]; } } } 

当我打开应用程序的默认背景是黑色,当我刷在右边它显示blue_back.png或蓝色的背景,当我向后滑动显示默认背景是黑色的,当它再次黑色,我会刷卡它离开它显示red_back.png或红色的背景,反之亦然。 现在我想要的是,我将添加另一个红色旁边的黄色背景的页面,所以当IM在默认的黑色页面,当我向左滑动两次显示黄色。 还有滑动function,当我滑动它可能需要一秒钟切换页面,我怎么能实现“实时滑动”像在表视图,但水平,所以它改变页面快。 谢谢!

更新回答:

你想要的是一个分页滚动视图。 诀窍是添加视图是在scrollView正确的位置,并正确设置scrollviews contentSize。 具有两个视图的示例:

 - (void)viewDidLoad { [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeBottom; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; scrollView.pagingEnabled = YES; scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.bounds.size.height); [self.view addSubview:scrollView]; UIView *redView = [[UIView alloc] init]; redView.frame = self.view.bounds; redView.backgroundColor = [UIColor redColor]; [scrollView addSubview:redView]; UIView *blueView = [[UIView alloc] init]; CGRect frame = self.view.bounds; frame.origin.x = 320; blueView.frame = frame; blueView.backgroundColor = [UIColor blueColor]; [scrollView addSubview:blueView]; } 

老答案:我会更容易利用手势识别器的构build。 特别是UIPanGestureRecognizer。

例如:在viewDidLoad中:

 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)]; [self.view addGestureRecognizer:pan]; 

而行动:

 - (void) myAction:(UIPanGestureRecognizer*)gestureRec { if (gestureRec.state == UIGestureRecognizerStateEnded) { NSLog(@"ended"); NSLog(@"%@", NSStringFromCGPoint([gestureRec velocityInView:self.view])); } } 

状态决定何时结束。 您可以使用velocityInView来确定右/左和上/下移动