iOS刷卡功能

iOS刷卡功能,ios,swipe,Ios,Swipe,你好!我有一个问题,我有一个单页应用程序的代码 - (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 < e

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

- (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"];
        }

    }
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
CGPoint endPosition=[触摸位置视图:self.view];
如果(self.imageStatus>0)
{
if(self.startPosition.x
当我打开应用程序时,默认背景为黑色,当我向右滑动时,显示蓝色背景或蓝色背景,当我向后滑动时,显示默认背景为黑色,当再次打开黑色时,我将向左滑动时,显示红色背景或红色背景,反之亦然。现在我想要的是,我将在红色旁边添加另一个黄色背景的页面,所以当我在默认的黑色页面中时,当我向左滑动两次时,它会显示黄色。还有刷卡功能,当我刷卡时,切换一个页面可能需要一秒钟的时间,我如何实现“实时刷卡”,就像在表视图中一样,但是水平,所以它可以快速更改页面。谢谢

最新答案:

您需要的是分页滚动视图。诀窍是将视图添加到scrollView中的正确位置,并正确设置scrollviews内容大小。具有两个视图的示例:

- (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];
}
旧答案: 我更容易使用内置手势识别器。特别是UIPangestureRecognitor

例如: 在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]));
    }
}

该状态根据需要确定其结束时间。您可以使用VelocityView来确定右/左和上/下移动

似乎过于复杂,为什么不使用UIPangestureRecognitor或UIScrollView中的内置分页功能呢?您已经用touchesEnded方法编写了代码,因此,在未完成滑动之前,不会发生任何事情。当您将手指从屏幕上移开时,将执行此方法。我尝试将代码置于touchesBegin下,但我得到了相同的结果,页面在我松开手指后移动。我有点不确定您是否希望在抬起手指或跟随手指时立即进行更改?它应该跟随手指,就像在主屏幕中,如果你滑动到下一页,我会尝试添加更多类似这样的视图
UIView*blackiew=[[UIView alloc]init];CGRect frame=self.view.bounds;frame.origin.x=320;blueView.frame=frame;blueView.backgroundColor=[UIColor blueColor];[滚动视图添加子视图:blackView];UIView*yellowView=[[UIView alloc]init];CGRect frame=self.view.bounds;frame.origin.x=320;blueView.frame=frame;blueView.backgroundColor=[UIColor blueColor];[滚动视图添加子视图:黄色视图]但是我遇到了一些错误,如果不知道是什么错误,我就帮不了你。在我看来,您需要仔细阅读UIScrollView以及如何使用它们。要想制作iOS应用程序,必须了解它们。