1

As described in this answer, it is possible to change, for example, the color of a progress bar using a Content Filter (in this specific case, the "Hue Adjust" Content Filter).

It seems like you can only achieve this from within Xcode's Interface Builder; however, I'd like to programmatically enable/disable Content Filters. So, for example, I'd like to enable the Hue Adjust Content Filter so that the progress bar turns is green, and disable it sometime later from within a function, so that goes back to blue. Is this possible?

Skeleton Bow
  • 479
  • 1
  • 9
  • 22
  • That answer is for macOS and `NSView` files. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/SettingUpLayerObjects/SettingUpLayerObjects.html I tried several things - linking to QuartzCore and Quartz, adding a nib file, and no matter what - for an iOS project the `Effects Inspector` wasn't found. For a macOS project it does. BUT - you *should* be able to either (a) access the track tint in IB or (b) do exactly what you are looking to do in code instead of IB. –  Jul 26 '17 at 14:18
  • Hey, sorry if I wasn't clear (I've only got it as a tag) but I *am* actually talking about macOS. However, I could try showing two progress bars which look different, and hide one or the other. Not a nice solution, but it will work as far as I can tell – Skeleton Bow Jul 26 '17 at 19:01
  • Using Xcode 8.3 I created a very simple macOS project (Cocoa Application), opened the storyboard, clicked on the view controller, and the Effects Inspector button (last on the right) was there. Clicking the "+" button on "Filters" dropdown gave me a slew of things to choose from, including "Hue Adjust". Is that what you couldn't find? –  Jul 26 '17 at 20:02
  • That is indeed what I wanted, but I wanted to be able to enable/disable that hue programatically. – Skeleton Bow Jul 26 '17 at 20:15
  • I'm pretty much an iOS developer, but I actually *refuse* to use IB! (Old-timer, been in IT since 1984). I've found that anything you can do in IB you should be able to do in code. I use CoreImage a lot, and I'm guessing between using a progress view's `CALayer` and importing `QuartzCore`, you may the tools you need to code against the hue. –  Jul 26 '17 at 20:20
  • Ah, I see. I'm a newbie, so I know nothing *but* the interface builder. I'm sure that the hue is worked in by code somehow, and there definitely should be a way to change the value in code (for example, from 0 to 1 to 2 to 0), but I couldn't find anything about it (even searching "xcode content filters" doesn't return many relevant results). Maybe I'm trying to run before I can walk :) and thanks for trying to help :) – Skeleton Bow Jul 26 '17 at 20:45
  • Last comments. Describe for me what you want - here's what I read so far. The default track uses blue (on the minimum track only?) and you want something else. (Keep in mind, in iOS there are two tracks - minimum and maximum and I'm not sure that equates to macOS.) Do you want the "left" side to turn to green as it reaches some threshold, be it maximum or something below? And you wish to disable it. If that's it, it probably should take creating a subclass, so that the ones you want blue are the "NS" class and the others are the new subclass. –  Jul 26 '17 at 21:22
  • Really, it's fine – you don't need to. I'm just doing this for fun, and I don't want to take your time. For completeness though, I'm making an indeterminate progress bar. However, I was indeed able to "solve" this problem by creating a graphite progress bar, and hiding/showing the actual one on top of that :) – Skeleton Bow Jul 27 '17 at 07:17

2 Answers2

1

After a false start, it seems like the answer is real simple:

// Need to set this in code. The settings in IB don't carry over when
// I change the contentFilters
progressIndicator.minValue = 0
progressIndicator.maxValue = 100
progressIndicator.doubleValue = 50

let hueAdjust = CIFilter(name: "CIHueAdjust", withInputParameters: ["inputAngle": NSNumber(value: 1.7)])!
progressIndicator.contentFilters = [hueAdjust]
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Thanks. This helps. Just wondering what the magic number '1.7' is. How do we determine the value for a specific color. – KamyFC Sep 22 '18 at 09:31
0

You can use the progressTintColor property of the progress view.

progressView.progressTintColor = .green
Casper Zandbergen
  • 3,419
  • 2
  • 25
  • 49