通过滑动更改没有viewcontroller的UiView

时间:2015-03-04 15:29:31

标签: ios objective-c iphone xcode ipad

我正在尝试更新继承的xCode项目。基本上我正在将一个动作附加到我已集成到项目中的滑动手势中。我以前只用故事板做过这个,但由于项目的年龄,这不是一个选择。

目前我有一个视图控制器,其中包含一个单独的UIView弹出窗口。然后,用户将在三页内容中来回滑动。还有另一个弹出窗口,但这只是一个页面,因此不需要该功能。我可以看到设备正在识别我的滑动(通过输出),但我不知道如何附加操作以导航到控制器中保存的其他UI视图.......或者如果这是偶数可能,我将不得不重新思考我是如何实现这一点的。任何帮助表示赞赏。

由于

1 个答案:

答案 0 :(得分:0)

你真的应该使用UIPageViewController,但如果你不想使用它,这里是一个代码示例:

@interface ViewController ()

@property (strong, nonatomic) UIView *view1;
@property (strong, nonatomic) UIView *view2;
@property (strong, nonatomic) UIView *view3;
@property (nonatomic) BOOL isSwipeEnabled;

@end

@implementation ViewController

- (void)viewDidLoad
{
    self.isSwipeEnabled = YES;

    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.view1];

    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.view1.frame), 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.view2.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.view2];

    self.view3 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.view2.frame), 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.view3.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.view3];

    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftGesture];

    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightGesture];
}

-(void)swipeHandler:(UISwipeGestureRecognizer*)recognizer
{
     self.isSwipeEnabled = NO;
    if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        if (self.view3.frame.origin.x > 0) {
            [self updateMyViewsToXPosition:self.view1.frame.origin.x - self.view.frame.size.width];
        }
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        if (self.view1.frame.origin.x != 0) {
              [self updateMyViewsToXPosition:self.view1.frame.origin.x + self.view.frame.size.width];
        }

    } else {
         self.isSwipeEnabled = YES;
    }
}

- (void)updateMyViewsToXPosition:(CGFloat)xPos
{
    [UIView animateWithDuration:0.3 animations:^{

        self.view1.frame = CGRectMake(xPos, 0, self.view1.frame.size.width, self.view1.frame.size.height);
        self.view2.frame = CGRectMake(CGRectGetMaxX(self.view1.frame), 0, self.view.frame.size.width, self.view.frame.size.height);
        self.view3.frame = CGRectMake(CGRectGetMaxX(self.view2.frame), 0, self.view.frame.size.width, self.view.frame.size.height);

    }completion:^(BOOL finished) {
         self.isSwipeEnabled = YES;
    }];
}