web-dev-qa-db-ja.com

iPhoneアプリでポップオーバーを作成するにはどうすればよいですか?

IPhoneでポップオーバーを探しています。iOS5リーダー機能のようにしたいと思います。

enter image description here

少し調べてみたところ、WEPopoverとFPPopoverが見つかりましたが、このAPI組み込みのiphoneSDKのようなものがあるかどうかを探しています。

14
Xtrician

カスタムアートワークを使用してUIViewを作成し、次のようなボタンを使用して、ビューの上にアニメーションで「ポップオーバー」として表示できます。

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 100, 50)]; //<- change to where you want it to show.

//Set the customView properties
customView.alpha = 0.0;
customView.layer.cornerRadius = 5;
customView.layer.borderWidth = 1.5f;
customView.layer.masksToBounds = YES;

//Add the customView to the current view
[self.view addSubview:customView];

//Display the customView with animation
[UIView animateWithDuration:0.4 animations:^{
    [customView setAlpha:1.0];
} completion:^(BOOL finished) {}];

#import <QuartzCore/QuartzCore.h>を使用する場合は、customView.layerを忘れないでください。

26

IOS8以降、ポップオーバーを作成できるようになりました。これは、iPhoneでもiPadでも同じです。これは、ユニバーサルアプリを作成する人にとって特に素晴らしいので、個別のビューやコードを作成する必要はありません。

ここでクラスとデモプロジェクトを取得できます: https://github.com/soberman/ARSPopover

サブクラスUIViewControllerを実行し、UIPopoverPresentationControllerDelegateプロトコルに準拠し、modalPresentationStyle値とともに目的のdelegateを設定するだけです。

// This is your CustomPopoverController.m

@interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>

@end

@implementation CustomPopoverController.m

- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.delegate = self;
    }
    return self;
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
}

その後、表示するメソッドで新しく作成したサブクラスをインスタンス化し、さらに2つの値をsourceViewsourceRectに割り当てます。次のようになります。

CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];

そして、あなたはそれを持っています、素敵な、きちんとしたぼやけたポップオーバー。

12
Soberman