Skip to content

Commit 4e4abc7

Browse files
authored
Update iosOrientationExample with UIAlertController (#7880)
#changelog #ios
1 parent 120bfb6 commit 4e4abc7

5 files changed

Lines changed: 111 additions & 80 deletions

File tree

examples/ios/iosOrientationExample/src/ActionSheetDelegateForOF.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/ios/iosOrientationExample/src/ActionSheetDelegateForOF.mm

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AlertControllerForOF.h
3+
// iosOrientationExample
4+
//
5+
// Created by azuremous on 23/01/24
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
#import "ofApp.h"
10+
11+
/**
12+
This is a piece of Objective-C code that will assist in linking the OF app with the UIAlertController. All it does is reroute UI events from the UIAlertController to the OF app.
13+
*/
14+
15+
@interface AlertControllerForOF : NSObject {
16+
ofApp * app;
17+
}
18+
19+
- (id)initWithApp:(ofApp *)app;
20+
- (void)showRotationOptions;
21+
@end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// AlertControllerForOF.cpp
3+
// iosOrientationExample
4+
//
5+
// Created by azuremous on 23/01/24
6+
//
7+
8+
#import "AlertControllerForOF.h"
9+
10+
@implementation AlertControllerForOF {
11+
12+
}
13+
14+
- (id)initWithApp:(ofApp *)myApp {
15+
self = [super init];
16+
if(self) {
17+
app = myApp;
18+
}
19+
return self;
20+
}
21+
22+
- (void)dealloc {
23+
24+
}
25+
26+
- (void)showRotationOptions {
27+
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
28+
message:@"Choose rotation"
29+
preferredStyle:UIAlertControllerStyleActionSheet];
30+
31+
UIAlertAction *portraitAction = [UIAlertAction actionWithTitle:@"Portrait"
32+
style:UIAlertActionStyleDefault
33+
handler:^(UIAlertAction *action) {
34+
self->app->rotateToPortrait();
35+
}];
36+
[alertController addAction:portraitAction];
37+
38+
UIAlertAction *portraitUpsideDownAction = [UIAlertAction actionWithTitle:@"Portrait Upside Down"
39+
style:UIAlertActionStyleDefault
40+
handler:^(UIAlertAction *action) {
41+
self->app->rotateToPortraitUpSideDown();
42+
}];
43+
[alertController addAction:portraitUpsideDownAction];
44+
45+
UIAlertAction *landscapeLeftAction = [UIAlertAction actionWithTitle:@"Landscape Left"
46+
style:UIAlertActionStyleDefault
47+
handler:^(UIAlertAction *action) {
48+
self->app->rotateToLandscapeLeft();
49+
}];
50+
51+
[alertController addAction:landscapeLeftAction];
52+
53+
UIAlertAction *landscapeRightAction = [UIAlertAction actionWithTitle:@"Landscape Right"
54+
style:UIAlertActionStyleDefault
55+
handler:^(UIAlertAction *action) {
56+
self->app->rotateToLandscapeRight();
57+
}];
58+
[alertController addAction:landscapeRightAction];
59+
60+
UIAlertAction *autoRotationAction = [UIAlertAction actionWithTitle:@"Toggle Auto Rotation"
61+
style:UIAlertActionStyleDefault
62+
handler:^(UIAlertAction *action) {
63+
self->app->toggleAutoRotation();
64+
65+
}];
66+
[alertController addAction:autoRotationAction];
67+
68+
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
69+
style:UIAlertActionStyleCancel
70+
handler:nil];
71+
[alertController addAction:cancelAction];
72+
73+
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
74+
UIView * sourceView = ofxiOSGetGLParentView();
75+
if (alertController.popoverPresentationController) {
76+
alertController.popoverPresentationController.sourceView = sourceView;
77+
alertController.popoverPresentationController.sourceRect = CGRectMake(sourceView.bounds.size.width/2, sourceView.bounds.size.height/2, 0, 0);
78+
alertController.popoverPresentationController.permittedArrowDirections = 0;
79+
}
80+
}
81+
82+
[ofxiOSGetUIWindow().rootViewController presentViewController:alertController animated:YES completion:nil];
83+
}
84+
85+
86+
@end

examples/ios/iosOrientationExample/src/ofApp.mm

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ofApp.h"
22
#include "ofAppiOSWindow.h"
3-
#import "ActionSheetDelegateForOF.h"
3+
#import "AlertControllerForOF.h"
44

55
//--------------------------------------------------------------
66
void ofApp::setup(){
@@ -143,15 +143,9 @@
143143
autoTitle = @"Turn Auto Rotation ON";
144144
}
145145

146-
UIActionSheet * actionSheet;
147-
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Orientation"
148-
delegate:nil
149-
cancelButtonTitle:@"Cancel"
150-
destructiveButtonTitle:nil
151-
otherButtonTitles:@"Portrait", @"Portrait Upside Down", @"Landscape Left", @"Landscape Right", autoTitle, nil];
152-
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
153-
actionSheet.delegate = [[ActionSheetDelegateForOF alloc] initWithApp:this];
154-
[actionSheet showInView:ofxiOSGetGLParentView()];
146+
AlertControllerForOF * alertControllerForOF = [[AlertControllerForOF alloc] initWithApp:this];
147+
148+
[alertControllerForOF showRotationOptions];
155149
}
156150

157151
//--------------------------------------------------------------

0 commit comments

Comments
 (0)