Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

swift - In iOS, how do I create a button that is always on top of all other view controllers?

No matter if modals are presented or the user performs any type of segue.

Is there a way to keep the button "always on top" (not the top of the screen) throughout the entire app?

Is there any way to make this button draggable and snappable to the screen?

I'm looking at Apple's own Assistive Touch as an example of such button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by creating your own subclass of UIWindow, and then creating an instance of that subclass. You'll need to do three things to the window:

  1. Set its windowLevel to a very high number, like CGFloat.max. It's clamped (as of iOS 9.2) to 10000000, but you might as well set it to the highest possible value.

  2. Set the window's background color to nil to make it transparent.

  3. Override the window's pointInside(_:withEvent:) method to return true only for points in the button. This will make the window only accept touches that hit the button, and all other touches will be passed on to other windows.

Then create a root view controller for the window. Create the button and add it to the view hierarchy, and tell the window about it so the window can use it in pointInside(_:withEvent:).

There's one last thing to do. It turns out that the on-screen keyboard also uses the highest window level, and since it might come on the screen after your window does, it will be on top of your window. You can fix that by observing UIKeyboardDidShowNotification and resetting your window's windowLevel when that happens (because doing so is documented to put your window on top of all windows at the same level).

Here's a demo. I'll start with the view controller I'm going to use in the window.

import UIKit

class FloatingButtonController: UIViewController {

Here's the button instance variable. Whoever creates a FloatingButtonController can access the button to add a target/action to it. I'll demonstrate that later.

    private(set) var button: UIButton!

You have to “implement” this initializer, but I'm not going to use this class in a storyboard.

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

Here's the real initializer.

    init() {
        super.init(nibName: nil, bundle: nil)
        window.windowLevel = CGFloat.max
        window.hidden = false
        window.rootViewController = self

Setting window.hidden = false gets it onto the screen (when the current CATransaction commits). I also need to watch for the keyboard to appear:

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
    }

I need a reference to my window, which will be an instance of a custom class:

    private let window = FloatingButtonWindow()

I'll create my view hierarchy in code to keep this answer self-contained:

    override func loadView() {
        let view = UIView()
        let button = UIButton(type: .Custom)
        button.setTitle("Floating", forState: .Normal)
        button.setTitleColor(UIColor.greenColor(), forState: .Normal)
        button.backgroundColor = UIColor.whiteColor()
        button.layer.shadowColor = UIColor.blackColor().CGColor
        button.layer.shadowRadius = 3
        button.layer.shadowOpacity = 0.8
        button.layer.shadowOffset = CGSize.zero
        button.sizeToFit()
        button.frame = CGRect(origin: CGPointMake(10, 10), size: button.bounds.size)
        button.autoresizingMask = []
        view.addSubview(button)
        self.view = view
        self.button = button
        window.button = button

Nothing special going on there. I'm just creating my root view and putting a button in it.

To allow the user to drag the button around, I'll add a pan gesture recognizer to the button:

        let panner = UIPanGestureRecognizer(target: self, action: "panDidFire:")
        button.addGestureRecognizer(panner)
    }

The window will lay out its subviews when it first appears, and when it is resized (in particular because of interface rotation), so I'll want to reposition the button at those times:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        snapButtonToSocket()
    }

(More on snapButtonToSocket shortly.)

To handle dragging the button, I use the pan gesture recognizer in the standard way:

    func panDidFire(panner: UIPanGestureRecognizer) {
        let offset = panner.translationInView(view)
        panner.setTranslation(CGPoint.zero, inView: view)
        var center = button.center
        center.x += offset.x
        center.y += offset.y
        button.center = center

You asked for “snapping”, so if the pan is ending or cancelled, I'll snap the button to one of a fixed number of positions that I call “sockets”:

        if panner.state == .Ended || panner.state == .Cancelled {
            UIView.animateWithDuration(0.3) {
                self.snapButtonToSocket()
            }
        }
    }

I handle the keyboard notification by resetting window.windowLevel:

    func keyboardDidShow(note: NSNotification) {
        window.windowLevel = 0
        window.windowLevel = CGFloat.max
    }

To snap the button to a socket, I find the nearest socket to the button's position and move the button there. Note that this isn't necessarily what you want on an interface rotation, but I'll leave a more perfect solution as an exercise for the reader. At any rate it keeps the button on the screen after a rotation.

    private func snapButtonToSocket() {
        var bestSocket = CGPoint.zero
        var distanceToBestSocket = CGFloat.infinity
        let center = button.center
        for socket in sockets {
            let distance = hypot(center.x - socket.x, center.y - socket.y)
            if distance < distanceToBestSocket {
                distanceToBestSocket = distance
                bestSocket = socket
            }
        }
        button.center = bestSocket
    }

I put a socket in each corner of the screen, and one in the middle for demonstration purposes:

    private var sockets: [CGPoint] {
        let buttonSize = button.bounds.size
        let rect = view.bounds.insetBy(dx: 4 + buttonSize.width / 2, dy: 4 + buttonSize.height / 2)
        let sockets: [CGPoint] = [
            CGPointMake(rect.minX, rect.minY),
            CGPointMake(rect.minX, rect.maxY),
            CGPointMake(rect.maxX, rect.minY),
            CGPointMake(rect.maxX, rect.maxY),
            CGPointMake(rect.midX, rect.midY)
        ]
        return sockets
    }

}

Finally, the custom UIWindow subclass:

private class FloatingButtonWindow: UIWindow {

    var button: UIButton?

    init() {
        super.init(frame: UIScreen.mainScreen().bounds)
        backgroundColor = nil
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

As I mentioned, I need to override pointInside(_:withEvent:) so that the window ignores touches outside the button:

    private override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        guard let button = button else { return false }
        let buttonPoint = convertPoint(point, toView: button)
        return button.pointInside(buttonPoint, withEvent: event)
    }

}

Now how do you use this thing? I downloaded Apple's AdaptivePhotos sample project and added my FloatingButtonController.swift file to the AdaptiveCode target. I added a property to AppDelegate:

var floatingButtonController: FloatingButtonController?

Then I added code to the end of application(_:didFinishLaunchingWithOptions:) to create a FloatingButtonController:

    floatingButtonController = FloatingButtonController()
    floatingButtonController?.button.addTarget(self, action: "floatingButtonWasTapped", forControlEvents: .TouchUpInside)

Those lines go right before the return true at the end of the function. I also need to write the action method for the button:

func floatingButtonWasTapped() {
    let alert = UIAlertController(title: "Warning", message: "Don't do that!", preferredStyle: .Alert)
    let action = UIAlertAction(title: "Sorry…", style: .Default, handler: nil)
    alert.addAction(action)
    window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}

That's all I had to do. But I did one more thing for demonstration purposes: in AboutViewController, I changed label to a UITextView so I'd have a way to bring up the keyboard.

Here's what the button looks like:

dragging the button

Here's the effect of tapping the button. Notice that the button floats above the alert:

clicking the button

Here's what happens when I bring up the keyboard:

keyboard

Does it handle rotation? You bet:

rotation

Well, the rotation-handling isn't perfect, because which socket is nearest the button after a rotation may not be the same “logical” socket as before the rotation. You could fix this by keeping track of which socket the button was last snapped to, and handling a rotation specially (by detecting the size change).

I put the entire FloatingViewController.swift in this gist for your convenience.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...