gpt4 book ai didi

ios - LoadView 永远不会在 UIViewController 上调用

转载 作者:行者123 更新时间:2023-11-29 11:35:35 24 4
gpt4 key购买 nike

我正在尝试复制实现 In iOS, how do I create a button that is always on top of all other view controllers?在 Objective-C 中。

但是 loadView 从未被调用,有人能告诉我为什么吗?以及如何解决?

ApplicationFloatingButtonWindow.m

#import "ApplicationFloatingButtonWindow.h"
#import "ApplicationFloatingButtonController.h"

@interface ApplicationFloatingButtonWindow()

#pragma mark - Properties
@property (strong, nonatomic) UIButton *floatingButton;
@property (strong, nonatomic) ApplicationFloatingButtonController *floatingButtonViewController;

@end

@implementation ApplicationFloatingButtonWindow

#pragma mark - Initialization
- (instancetype)init {
self = [super init];
if (self) {
self.backgroundColor = nil; // make background transparent
}
return self;
}

#pragma mark - Gesture Handling
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if(self.floatingButton) {
CGPoint floatingButtonPoint = [self convertPoint:point toView:self.floatingButton];
return [self.floatingButton pointInside:floatingButtonPoint withEvent:event];
}
return [super pointInside:point withEvent:event];
}

@end

ApplicationFloatingButtonController.h

#import <UIKit/UIKit.h>
#import "ApplicationFloatingButtonWindow.h"

@interface ApplicationFloatingButtonController : UIViewController

#pragma mark - Properties
@property (strong, nonatomic) UIButton *floatingButton;
@property (strong, nonatomic) ApplicationFloatingButtonWindow *window;

@end

ApplicationFloatingButtonController.m

#import "ApplicationFloatingButtonController.h"

@interface ApplicationFloatingButtonController ()

@end

@implementation ApplicationFloatingButtonController

#pragma mark - Initialization
- (instancetype)init {
self = [super initWithNibName:nil bundle:nil];
if (self) {
[self setup];
}
return self;
}

#pragma mark - Setup
- (void)setup {
self.window.windowLevel = CGFLOAT_MAX;
self.window.hidden = NO;
self.window.rootViewController = self;
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
}

- (void)setupPanGestureRecognizers {
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
[self.floatingButton addGestureRecognizer:panGestureRecognizer];
}

#pragma mark - Lifecycle
- (void)loadView {
UIButton *button = [self createButton];
UIView *view = [UIView new];
[view addSubview:button];
self.view = view;
self.floatingButton = button;
[self setupPanGestureRecognizers];
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self snapButtonToBestPosition];
}

#pragma mark - Elements Factory
- (UIButton *)createButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setTitle:@"Floating..." forState:UIControlStateNormal];
[button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
button.backgroundColor = UIColor.whiteColor;
button.layer.shadowColor = UIColor.blackColor.CGColor;
button.layer.shadowRadius = 3;
button.layer.shadowOpacity = 0.8;
button.layer.shadowOffset = CGSizeZero;
[button sizeToFit];
button.frame = CGRectMake(10, 10, 24, 24);
button.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);;
return button;
}

#pragma mark - Selectors
- (void)keyboardDidShow {
// This refreshes the window level and puts it back to the top most view
self.window.windowLevel = 0;
self.window.windowLevel = CGFLOAT_MAX;
}

- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint gestureOffset = [panGestureRecognizer translationInView:self.view];
[panGestureRecognizer setTranslation:CGPointZero inView:self.view];
CGPoint newButtonCenter = self.floatingButton.center;
newButtonCenter.x += gestureOffset.x;
newButtonCenter.y += gestureOffset.y;
self.floatingButton.center = newButtonCenter;

if (panGestureRecognizer.state == UIGestureRecognizerStateEnded || panGestureRecognizer.state == UIGestureRecognizerStateCancelled ) {
[UIView animateWithDuration:0.3 animations:^{
[self snapButtonToBestPosition];
}];
}

}

#pragma mark - Elements Layout and Positioning Methods
- (NSArray *)possibleButtonPositions {

CGSize buttonSize = self.floatingButton.bounds.size;
CGRect rect = CGRectInset(self.view.frame, 4 + buttonSize.width / 2, 4 + buttonSize.height / 2);

NSMutableArray *possiblePositions = [NSMutableArray new];
[possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))]];
[possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect))]];
[possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))]];
[possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect))]];
[possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))]];

return possiblePositions;
}

- (void)snapButtonToBestPosition {
CGPoint bestPositionForButton = CGPointZero;
CGFloat distanceToBestPosition = CGFLOAT_MAX;
CGPoint buttonCenter = self.floatingButton.center;
for (NSValue *possibleButtonPositionAsNSValue in [self possibleButtonPositions]) {
CGPoint possibleButtonPosition = possibleButtonPositionAsNSValue.CGPointValue;
CGFloat distance = hypot(buttonCenter.x - possibleButtonPosition.x, buttonCenter.y - possibleButtonPosition.y);
if (distance < distanceToBestPosition) {
distanceToBestPosition = distance;
bestPositionForButton = possibleButtonPosition;
}
}
self.floatingButton.center = bestPositionForButton;
}

@end

AppDelegate.m

#import "AppDelegate.h"
#import "ApplicationFloatingButtonController.h"

@interface AppDelegate ()

#pragma mark - Properties
@property (strong, nonatomic) ApplicationFloatingButtonController *floatingButtonController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// Configure Floating Button
self.floatingButtonController = [ApplicationFloatingButtonController new];
[self.floatingButtonController.floatingButton addTarget:self action:@selector(floatingButtonDidReceiveTouchUpInside) forControlEvents:UIControlEventTouchUpInside];

return YES;
}

- (void)floatingButtonDidReceiveTouchUpInside {

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];

}

@end

解决方案和最终代码在这里:

https://github.com/bocato/ApplicationFloatingButton

最佳答案

Swift 版本有

private let window = FloatingButtonWindow() 

FloatingButtonController 类中。您已经在 .h 文件中创建了该属性,但您没有在任何地方对其进行初始化。尝试添加

self.window = [ApplicationFloatingButtonWindow new]; 

到您的设置方法。

关于ios - LoadView 永远不会在 UIViewController 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49560500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com