簡體   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