I have a fuction, which after a shake, resets all sliders to their defaults and calls -proccesImage after the slider's value has been changed. The problem is that each of the sliders do, in fact, get reset to the default positions but GPUImage does not update the view.
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?)
{
if(event?subtype) == UIEventSubtype.MotionShake{
for slider in sliders!
{
slider.value = myDefault(slider.tag);
sliderValueChanged(slider)
}
}
}
#IBAction func sliderValueChanged(sender: UISlider)
{
switch sender.tag{
case 1:
filter.temperature = CGFloat(sender.value)
case 2:
dliter2.exposure = CGFloat(sender.value)
case 3:
//more cases..
default:print("...")
}
gpuimage?.processImage(); // I know for a fact that this gets called, but doesn't update the view :(
}
Note that I also have an identical functionality attached to UITap gesture. When a user double taps a slider, it goes back to its default value and updates right away. Code below works perfectly fine.
func resetSlider(sender: UITapGestureRecognizer)
{
let tag = (sender.view?.tag)!;
sliders![tag].value = myDefault(tag)
sliderValueChanged(sliders![tag]);
}
So I haven't found a reason why it wasn't updating. But I solved the issue by removing all targets and calling viewWillAppear() after that, so that it would re-add the targets just as if it was a brand new image.
Related
Is it possible to detect a finger pan on a NSTouchBarView?
Sorry for the lack of code but I don't even know where to start.
MacOS is not made for finger touches but the TouchBar is but I do not see how to do it on a NSTouchBarView
I don't know specifically about using NSTouchBarView, but using a pan recognizer in a touch bar usually works like this: create a view, then create a NSPanGestureRecognizer (don't forget to set the target and action) then add the recognizer to the previously created view. Finally, create your NSCustomTouchBarItem and assign the previously created view to the item's view. Quick example in Swift:
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
switch identifier {
case NSTouchBarItemIdentifier.yourCustomItem:
return itemWithRecognizer(identifier: identifier)
default:
return nil
}
}
func itemWithRecognizer(identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem {
let customView = NSView()
customView.wantsLayer = true
let recognizer = NSPanGestureRecognizer()
recognizer.target = self
recognizer.action = #selector(doSomething)
customView.addGestureRecognizer(recognizer)
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = customView
return item
}
func doSomething() {
// gesture was activated
}
I'm programming an app for tvOS and am using a UISegmentedControl. My issue is that as I'm scrolling through items, it sends its action method as soon as I remain on an item for a second or two. I would like to disable this behaviour and rather fire the action only if user clicks it.
Does anyone know how to do it?
I had the same problem with my tvOS app and solved with this:
#IBOutlet weak var searchSegmentedControl: UISegmentedControl!
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(didClickSegmentedSearchButton))
tap.numberOfTapsRequired = 1
view.addGestureRecognizer(tap)
}
func didClickSegmentedSearchButton() {
switch searchSegmentedControl.selectedSegmentIndex {
case 0:
// Restaurant
case 1:
// Hotel
case 2:
// Attraction
case 3:
// Shopping
default:
// Unknown
}
}
I have a NSWindow, on which i apply this:
window.styleMask = window.styleMask | NSFullSizeContentViewWindowMask
window.titleVisibility = NSWindowTitleVisibility.Hidden;
window.titlebarAppearsTransparent = true;
I then add a NSView behind the titlebar to simulate a bigger one.
Now it looks like this:
I want to be able to move the window, by dragging the light-blue view. I have already tried to subclass NSView and always returning true for mouseDownCanMoveWindow using this code:
class LSViewD: NSView {
override var mouseDownCanMoveWindow:Bool {
get {
return true
}
}
}
This didn't work.
After some googling i found this INAppStoreWindow on GitHub. However it doesn't support OS X versions over 10.9, so it's completely useless for me.
Edit1
This is how it looks in the Interface Builder.
How can i move the window, by dragging on this NSView?
None of the answers here worked for me. They all either don't work at all, or make the whole window draggable (note that OP is not asking for this).
Here's how to actually achieve this:
To make a NSView control the window with it's drag events, simply subclass it and override the mouseDown as such:
class WindowDragView: NSView {
override public func mouseDown(with event: NSEvent) {
window?.performDrag(with: event)
}
}
That's it. The mouseDown function will transfer further event tracking to it's parent window.
No need for window masks, isMovableByWindowBackground or mouseDownCanMoveWindow.
Try setting the window's movableByWindowBackground property to true.
There are two ways to do this. The first one would be to set the NSTexturedBackgroundWindowMask as well as the windows background color to the one of your view. This should work.
Otherwise you can take a look at this Sample Code
I somehow managed to solve my problem, i don't really know how, but here are some screenshots.
In the AppDelegate file where i edit the properties of my window, i added an IBOutlet of my contentView. This IBOutlet is a subclass of NSView, in which i've overriden the variable mouseDownCanMoveWindow so it always returns false.
I tried this before in only one file, but it didn't work. This however solved the problem.
Thanks to Ken Thomases and Max for leading me into the right direction.
Swift3.0 Version
override func viewDidAppear() {
//for hide the TitleBar
self.view.window?.styleMask = .borderless
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.titleVisibility = .hidden
//for Window movable with NSView
self.view.window?.isMovableByWindowBackground = true
}
Swift 3:
I needed this but dynamically. It's a little long but well worth it (IMHO).
So I decided to enable this only while the command key is down. This is achieved by registering a local key handler in the delegate:
// MARK:- Local key monitor
var localKeyDownMonitor : Any? = nil
var commandKeyDown : Bool = false {
didSet {
let notif = Notification(name: Notification.Name(rawValue: "commandKeyDown"),
object: NSNumber(booleanLiteral: commandKeyDown))
NotificationCenter.default.post(notif)
}
}
func keyDownMonitor(event: NSEvent) -> Bool {
switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) {
case [.command]:
self.commandKeyDown = true
return true
default:
self.commandKeyDown = false
return false
}
}
which is enabled within the delegate startup:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Watch local keys for window movenment, etc.
localKeyDownMonitor = NSEvent.addLocalMonitorForEvents(matching: NSEventMask.flagsChanged) { (event) -> NSEvent? in
return self.keyDownMonitor(event: event) ? nil : event
}
}
and its removal
func applicationWillTerminate(_ aNotification: Notification) {
// Forget key down monitoring
NSEvent.removeMonitor(localKeyDownMonitor!)
}
Note that when the commandKeyDown value is changed by the key down handler. This value change is caught by the didset{} to post a notification. This notification is registered by any view you wish to have its window so moved - i.e., in the view delegate
override func viewDidLoad() {
super.viewDidLoad()
// Watch command key changes
NotificationCenter.default.addObserver(
self,
selector: #selector(ViewController.commandKeyDown(_:)),
name: NSNotification.Name(rawValue: "commandKeyDown"),
object: nil)
}
and discarded when the viewWillDisappear() (delegate) or the window controller windowShouldClose(); add this
<your-view>.removeObserver(self, forKeyPath: "commandKeyDown")
So sequence goes like this:
key pressed/release
handler called
notification posted
The view's window isMovableByWindowBackground property is changed by notification - placed within view controller / delegate or where you registered the observer.
internal func commandKeyDown(_ notification : Notification) {
let commandKeyDown : NSNumber = notification.object as! NSNumber
if let window = self.view.window {
window.isMovableByWindowBackground = commandKeyDown.boolValue
Swift.print(String(format: "command %#", commandKeyDown.boolValue ? "v" : "^"))
}
}
Remove the tracer output when happy. See it in action in SimpleViewer on github.
I make a simple WKInterface with a switch control and make a function for that switch to show up the value of switch like this:
I don't know why when test the switch,the result I received always false? please explain and show me how to fix. As I thought the out put of that switch will change from false to true and move on like that.. What's wrong in here? or the Xcode's bug with switch?
Here, instead of passing value of type Bool, pass it as a UISwitch:
#IBAction func testSwitchPressed(value: UISwitch) {
println(value.on)
}
This will correctly tell if if your switch is on or off.
This is great because you can use Control Flow as well with this:
#IBAction func testSwitchPressed(value: UISwitch) {
//println(value.on)
if value.on {
println("The switch has been enabled!")
//cool features here
} else {
println("The switch has been disabled!")
//other cool features here
}
}
Good luck!
Try this:
First declare outlet for switch like this:
#IBOutlet weak var mySwitch: UISwitch!
In viewDidLoad implement listener (target-action):
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mySwitch.addTarget(self, action: "switchChangedValue:", forControlEvents: UIControlEvents.ValueChanged)
}
Declare this function where you will get response every time switch value is changed:
func switchChangedValue(sender:UISwitch){
if sender.isKindOfClass(UISwitch){
if sender.on {
println("Switch is ON")
println("\(sender.on)")
} else {
println("Switch is OFF")
println("\(sender.on)")
}
}
}
the IBAction function passes the UIElement which triggered the action.
So use something like this
#IBAction func testSwitchPressed(sender : UISwitch)
{
println(sender.on)
}
If you press CTRL and drag the mouse in the InterfaceController source code, the following function is automatically added:
-(IBAction)TapOnSwitch:(BOOL)value
{
}
And value is correct.
This is valid if you want check the value when the user change the value of the switch.
I have some vars in my Main VC and when user clicks a button in another VC the prepareForSegue passes along a new value to the Main VC and updates a label.
But when the user clicks again it's back to initial value, so it doesn't increment since the value is set back in the viewDidLoad?
MainVC:
var statsHealth:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
healthLabel.text = String("Health: \(statsHealth)/10")
}
Another VC:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "startSegue") {
let startVC = segue.destinationViewController as ViewController
startVC.statsHealth += 1
}
It's displayed as 0, then 1 but then 0 again and then 1 instead of 2,3,4 etc.
Any ideas?
BR
Nils
Perhaps not the most 'Swift' way to do it, but certainly works well....
Create a file called Variables.swift which will hold all your 'universal' variables (if these are going to be on every page, I see no reason this isn't the 'best' way to do it - certainly it is the most simple to understand!)
in Variables.swift, hold all your universal variables
struct Variables {
static var statsHealth = 0
.....
}
Then, in each other page, access them at any time
healthLabel.text = String("Health: \(Variables.statsHealth)/10")
or set them
Variables.statsHealth += 1
So based on your description, I assume the view controller structure is like this:
AnotherVC -> MainVC
MainVC is presented on top of AnotherVC. When you go back to AnotherVC, did you dismiss MainVC completely? If so, then every time you go from AnotherVC to MainVC, it initiate a new ViewController, and the variables you saved before doesn't exist anymore.
If you want to keep this structure and change variables in MainVC, keep a reference of mainVC in AnotherVC. Then instead of connecting in storyboard, you may want to present it programmatically.
class AnotherVC {
var mainVC: MainVC?
func presentMainVC() {
var targetVC = UIViewController()
if self.mainVC != nil {
targetVC = self.mainVC
} else {
let storyboard = UIStoryboard(name: "Your-storyboard-name", bundle: nil)
targetVC: MainVC = storyboard.instantiateViewControllerWithIdentifier("The-main-VC-identifier") as MainVC
self.mainVC = targetVC
}
//you can change your variable here
mainVC.statsHealth += 1
self.presentViewController(self.mainVC, animated: true, completion: nil)
}
If you mainVC is on top of AnotherVC in any case, you can just revert the reference direction.