我想从主视图控制器的右侧创建一个滑出菜单。但是,我希望滑出菜单出现在主视图的顶部,而不是将其推到一侧。
我看过 ECSlidingViewController 2,它允许从任一侧滑出,但是滑出菜单在暴露的边缘上似乎被切断了。它创建了顶视图控制器向左或向右移动以显示其下方隐藏菜单的效果。我希望它看起来隐藏菜单滑出到主视图控制器上,并且我们可以看到它的左边缘,因此没有内容被切断。
这完全有可能吗? ECSlidingViewController 2 会为此工作吗?
编辑
这是我尝试过的:
要检测触摸开始:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
BBAppDelegate *myAppDelegate = (BBAppDelegate*)[UIApplication sharedApplication].delegate;
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *myViewArray = [[NSBundle mainBundle] loadNibNamed:@"FilterView" owner:self options:nil];
self.filterView = myViewArray[0];
[myAppDelegate.window addSubview:self.filterView];
if (CGRectContainsPoint(self.filterView.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
然后拖动:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (dragging) {
CGRect frame = self.filterView.frame;
frame.origin.x = self.filterView.frame.origin.x + touchLocation.x;
frame.origin.y = self.filterView.frame.origin.y + touchLocation.y;
self.filterView.frame = frame;
}
}
但是,当我开始触摸时,filterView 会替换所有视图,并且无论如何都不会滑动或动画。我虽然这是由于没有提供一个起点 - 所以我像这样设置了 self.filterView :
self.filterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 564)];
但是,更改这些值对显示时视图的位置/大小没有影响。
编辑 2
好的,我开始了一个新的测试项目,并设法通过我的触摸在屏幕上获得一个可以移动的视图。
我在用UIPanGestureRecognizer
像这样:
viewDidLoad
- (void)viewDidLoad
{ [超级 viewDidLoad];
NSArray *myViewArray = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
UIView *myView = myViewArray[0];
self.filterView = myView;
self.filterView.frame = CGRectMake(100, 100, 200, 200);
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self
action:@selector(dragging:)];
pan.delegate = self;
[self.filterView addGestureRecognizer:pan];
[self.view addSubview:self.filterView];
}
然后在拖动方法中:
-(void)dragging:(UIPanGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
NSLog(@"Received a pan gesture");
self.panCoord = [gesture locationInView:gesture.view];
}
CGPoint newCoord = [gesture locationInView:gesture.view];
float dX = newCoord.x-self.panCoord.x;
gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);
}
这允许视图仅水平移动。
几乎就在那里 - 我现在想弄清楚的是当用户抬起手指时让它捕捉到一个位置。以及当它的起始位置不在屏幕上时如何移动这个视图。
编辑 3
好的,所以我想控制出现在屏幕上的视图的速度和速度。如果用户快速滑动,视图会以他们滑动的速度进入。如果他们慢慢滑动,它就会慢慢到位。但是我也设置了最低时间 - 所以如果他们需要很长时间 - 它默认为最小时间。
这是代码:
{
float start;
float end;
NSDate *methodStart;
NSDate *methodEnd;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
methodStart = [NSDate date];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
start = location.x;}
-(void)dragging:(UIPanGestureRecognizer *)gesture{
if(gesture.state == UIGestureRecognizerStateBegan)
{
self.panCoord = [gesture locationInView:gesture.view];
}
CGPoint newCoord = [gesture locationInView:gesture.view];
float dX = newCoord.x-self.panCoord.x;
gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y, gesture.view.frame.size.width, gesture.view.frame.size.height);
if (gesture.state == UIGestureRecognizerStateEnded){
methodEnd = [NSDate date];
NSTimeInterval executionTime = [methodEnd timeIntervalSinceDate:methodStart];
double time = executionTime;
float distance = start - end;
int distanceOverTime = distance / time;
float veloticty = distanceOverTime / 100;
double miniTimeToUse = 0.5;
double newMinTime;
if (time < miniTimeToUse){
newMinTime = miniTimeToUse;
}else {
newMinTime = time;
}
if (gesture.view.frame.origin.x+dX >= 180){
[UIView animateWithDuration:newMinTime
delay:0
usingSpringWithDamping:0.9
initialSpringVelocity:veloticty
options:0
animations:^{
self.filterView.frame = CGRectMake(300, 100, 200, 200);
} completion:nil];
}
if (gesture.view.frame.origin.x+dX <= 181){
[UIView animateWithDuration:newMinTime
delay:0
usingSpringWithDamping:0.9
initialSpringVelocity:veloticty
options:0
animations:^{
self.filterView.frame = CGRectMake(50, 100, 200, 200);
} completion:nil];
}
}
}
现在只是为了让它从侧面拉开视野。