繁体   English   中英

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

[英]How to slide a button from the bottom after sliding from the edge?

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

-(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的新手,有些东西不太容易开发。

谁能给我一个好的建议?

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

可以说您正在视图控制器中执行此操作。 为点按钮创建一个属性。 在确实加载视图的情况下,创建按钮并将其分配给属性:

    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
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM