iOS Swiping functions

不羁的心 提交于 2019-12-12 02:53:43

问题


Good day! I have a question,I have this code for a Single Page application

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

    }
}

when i open the app the default background is black, when i swipe at right it shows the blue_back.png or the blue background, when i swipe back it shows the default background which is black, and when its on black again and i will swipe it left it shows the red_back.png or the red background and vise versa. now what i want is, i will add another page with yellow background next to red, so when im in the default black page, when i swipe to left two times it shows the yellow. and also the swiping function, when i swipe it it takes maybe a second to switch a page, how can i implement the "Realtime Swiping" like in the table view, but horizontal, so it changes page fast. thanks!


回答1:


Updated answer:

What you want is a paging scrollview. The trick is to add the views are the correct positions in the scrollView and set the scrollviews contentSize correctly. Example with two views:

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

Old answer: I would be easier to make use of the build in gesture recognizers. Specifically UIPanGestureRecognizer.

Example: In viewDidLoad:

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

And the action:

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

The state determines when its ended as you want. And you can use the velocityInView to determine right/left and up/down movement



来源:https://stackoverflow.com/questions/20399635/ios-swiping-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!