メイン ビュー コントローラーの右側からスライド アウト メニューを作成したいと考えています。ただし、スライドアウトメニューをメインビューの上に表示し、片側に押し込まないようにしたい.
私は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
{ [super 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];
}
}
}
これで、ビューを横から引き離すことができるようになります。