[英]How to slide a button from the bottom after sliding from the edge?
I have implemented the following two methods 我已经实现了以下两种方法
-(void)addGestureToWebView {
[self setBottomEdgeGesture:[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleBottomEdgeGesture:)]];
[[self bottomEdgeGesture] setEdges:UIRectEdgeBottom];
[[self bottomEdgeGesture] setDelegate:self];
[[self webView] addGestureRecognizer:[self bottomEdgeGesture]];
}
and 和
-(IBAction)handleBottomEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == [gesture state] || UIGestureRecognizerStateChanged == [gesture state]) {
//Do something
}
}
The method handleBottomEdgeGesture
should slide a button with three dots from the bottom. 方法
handleBottomEdgeGesture
应该从底部滑动三个点的按钮。
I tried 我试过了
UIButton *btnDots = [[UIButton alloc] initWithFrame:CGRectMake(280.0, 528.0, 20, 20)];
[btnDots setTitle:@"..." forState:UIControlStateNormal];
[btnDots setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
but nothing happened. 但是什么也没发生。 I didn't see any dots at my display.
我的显示器上没有看到任何点。 I am new to Objective-C and some things not so easy to develop.
我是Objective-C的新手,有些东西不太容易开发。
Can anyone give me a good advice? 谁能给我一个好的建议?
Since you created a button programmatically you also need to add it to sub view. 由于您以编程方式创建了按钮,因此还需要将其添加到子视图中。 Then on gesture recognizer you should change the frame of the button.
然后在手势识别器上,您应该更改按钮的框架。 A reference to the button will be needed.
需要引用该按钮。
Lets say you are doing this in the view controller. 可以说您正在视图控制器中执行此操作。 Create a property for dot button.
为点按钮创建一个属性。 In view did load create the button and assign it to the property:
在确实加载视图的情况下,创建按钮并将其分配给属性:
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;
Then in gesture recognizer you have 2 options. 然后,在手势识别器中,您有2个选项。 Either just handle the event and show the button animated or move its frame as your finger moves:
只需处理事件并显示动画按钮即可,或者随着手指移动来移动其框架:
- (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
}
I hope you get the basic idea from the code. 希望您从代码中得到基本的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.