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

但是从不调用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];

设置方法。

07-26 09:37