从边缘滑动后如何从底部滑动按钮?

时间:2016-01-06 09:51:46

标签: ios objective-c uibutton

我已经实现了以下两种方法

-(void)addGestureToWebView {
    [self setBottomEdgeGesture:[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleBottomEdgeGesture:)]];
    [[self bottomEdgeGesture] setEdges:UIRectEdgeBottom];
    [[self bottomEdgeGesture] setDelegate:self];
    [[self webView] addGestureRecognizer:[self bottomEdgeGesture]];
}

-(IBAction)handleBottomEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
    if(UIGestureRecognizerStateBegan == [gesture state] || UIGestureRecognizerStateChanged == [gesture state]) {
        //Do something
    }
}

方法handleBottomEdgeGesture应该从底部滑动一个带有三个点的按钮。

我试过

UIButton *btnDots = [[UIButton alloc] initWithFrame:CGRectMake(280.0, 528.0, 20, 20)];
[btnDots setTitle:@"..." forState:UIControlStateNormal];
[btnDots setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

但没有发生任何事。我的显示器上没有任何圆点。我是Objective-C的新手,还有一些不太容易开发的东西。

有人能给我一个好建议吗?

1 个答案:

答案 0 :(得分:0)

由于您以编程方式创建了按钮,因此还需要将其添加到子视图中。然后在手势识别器上,您应该更改按钮的框架。需要引用该按钮。

假设您在视图控制器中执行此操作。为点按钮创建属性。在视图中,加载创建按钮并将其分配给属性:

    UIButton *dotButton = [[UIButton alloc] initWithFrame:CGRectMake(.0f, self.view.frame.size.height, self.view.frame.size.width, 20.0f)];
    // do additional button setup
    [self.view addSubview:dotButton];
    [dotButton addTarget:self action:@selector(onDotButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    self.dotButton = dotButton;

然后在手势识别器中,您有2个选项。要么只处理事件并显示动画按钮,要么在手指移动时移动框架:

- (void)onGesture:(UIGestureRecognizer *)sender
{
#ifdef USE_ONLY_ON_EVENT
    if(sender.state == UIGestureRecognizerStateBegan)
    {
        [UIView animateWithDuration:.23 animations:^{
            self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-self.dotButton.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
        }];
    }
#else
    if(sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint location = [sender locationInView:self.view];
        CGFloat yOffset = self.view.frame.size.height - location.y;
        if(yOffset > self.dotButton.frame.size.height)
        {
            yOffset = self.dotButton.frame.size.height;
        }
        self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-yOffset, self.view.frame.size.width, self.dotButton.frame.size.height);
    }
    else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled)
    {
        CGPoint location = [sender locationInView:self.view];
        CGFloat yOffset = self.view.frame.size.height - location.y;
        if(yOffset > self.dotButton.frame.size.height*.5f) // more then half of a button visible
        {
            // show full button
            [UIView animateWithDuration:.23 animations:^{
                self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-self.dotButton.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
            }];
        }
        else
        {
            // hide button
            [UIView animateWithDuration:.23 animations:^{
                self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
            }];
        }
    }
#endif
}

我希望你从代码中得到基本的想法。