/UIView-HoverEvents

Super easy hover gesture support for UIView

Primary LanguageSwift

🍃 UIView (HoverEvents)

Super easy hover gesture support for UIView (and UIView subclasses!)

Compatability:

  • iPadOS 13.0+
  • Mac Catalyst 13.0+

Usage:

0️⃣: Enable hover events by simply setting a BOOL on your UIView subclass!

Swift (from ColorView.swift):

override func awakeFromNib() {
	super.awakeFromNib()
		
	// just one line of code!
	self.tracksHoverEvents = true
}

Objective-C:

- (void)awakeFromNib {
	[super awakeFromNib];
	
	// still just one line of code!
	[self setTracksHoverEvents:YES];
}

1️⃣: You can also easily track the cursors position IN the view!

Swift (from TrackingView.swift):

override func cursorHoverBegan(_ pointInView: CGPoint) {
	// ...
}
	
override func cursorHoverChanged(_ pointInView: CGPoint) {
	// ...
}
	
override func cursorHoverEnded(_ pointInView: CGPoint) {
	// ...
}

override func cursorHoverCancelled(_ pointInView: CGPoint) {
	// ...
}

override func cursorHoverFailed(_ pointInView: CGPoint) {
	// ...
}

Objective-C:

- (void)cursorHoverBegan:(CGPoint)pointInView {
	// ...
}

- (void)cursorHoverChanged:(CGPoint)pointInView {
	// ...
}

- (void)cursorHoverEnded:(CGPoint)pointInView {
	// ...
}

- (void)cursorHoverCancelled:(CGPoint)pointInView {
	// ...
}

- (void)cursorHoverFailed:(CGPoint)pointInView {
	// ...
}

2️⃣: There's also simple support for highlighting a view when it's being hovered:

Swift:

self.hoveringBackgroundColor = UIColor.systemTeal

Objective-C:

[self setHoveringBackgroundColor:[UIColor systemTealColor]];

3️⃣: Don't forget to import the header file in your Bridging Header if you're using Swift!

(from HoverEvents-Bridging-Header.h):

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "UIView+HoverEvents.h"

Demo:

The demo app is built with Storyboards and the custom views are setup in ViewController.swift

Also here's a video demo I posted on my twitter: https://twitter.com/timdesir/status/1261178820335263744

Build, run, have fun!