if i click image long then func actionLongPress get started and as you can see, if i click image long then i want to move that image along my touch... but this code doesn't work...
there is no error. even though i click image long, image doesn't follow my touch..
do you fix this trouble???
#objc func actionLongPress(Recog : UILongPressGestureRecognizer){
if Recog.state == .began{
AudioServicesPlaySystemSound(1519)
addPanGesture(view: addedImage)
origin = addedImage.frame.origin
} else if Recog.state == .ended{
AudioServicesPlaySystemSound(1519)
}
}
You are missing one state that you need to implement ... which is state change
#objc func actionLongPress(Recog : UILongPressGestureRecognizer){
if Recog.state == .began{
AudioServicesPlaySystemSound(1519)
addPanGesture(view: addedImage)
origin = addedImage.frame.origin
} else if Recog.state == .changed {
// assign view frame here
let location = Recog.location(in: self)
your_view?.center = targetPosition
} else if Recog.state == .ended{
AudioServicesPlaySystemSound(1519)
}
}
Related
I am trying to hide a button, until another condition is met. When i load the app i am using the slider and when it reaches the max, it should make the button visible, but for some weird reason it won't. I have just done it in another app with no issues and the code is almost identical.?
Can't figure out what to do tbh.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
profileVisible.isHidden = true
}
#IBAction func btnClicked(_ sender: Any) {
motionManager.startDeviceMotionUpdates(to: queue) { (motion, error) in DispatchQueue.main.async {
self.slider.value = Float((motion?.attitude.roll ?? 0 ) * 1.4)
print(self.slider.value)
if self.slider.value == 1.0 {
self.profileVisible = false
self.motionManager.stopDeviceMotionUpdates()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // vibrates when payment has succeded
}
if self.slider.value == -1.0 {
profileVisible = true
self.motionManager.stopDeviceMotionUpdates()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
}
}
You miss setting here
if self.slider.value == 1.0 {
self.profileVisible = false
And here
if self.slider.value == -1.0 {
profileVisible = true
Should be
profileVisible.isHidden = true/false // set it's value according to your logic
profileVisible is of type UIButton you need to set it's isHidden property not it directly
This question already has an answer here:
Swift 4 save from the background color that was consulted in the App
(1 answer)
Closed 4 years ago.
I've got 4 SwichButton, this is standard to Off.
How can I save the setting when a switch button is set to on ?
I have this:
#IBAction func SwitchNofic(_ sender: UISwitch) {
let switchTag = sender.tag
if (switchTag == 1) && (sender.isOn == true){
print("1")
createNoficationMorgen()
}else if (switchTag == 2) && (sender.isOn == true){
print("2")
createNoficationMittag()
}else if (switchTag == 3) && (sender.isOn == true){
print("3")
createNoficationAbend()
}else if (switchTag == 4) && (sender.isOn == true){
print("4")
createNoficationNacht()
}
}
It depends if you want to save it just for the time the app is alive.
Just use a boolean variable so you know what is on and what is not, also each switchButton has a property isOn.
If you want it to be persistant try UserDefaults.
Swift 4
To save permanently :
UserDefaults.standard.set(true, forKey: “isDarkModeKey”)
To retrieve (this should be called in viewDidAppear):
let isDarkModeEnabled = UserDefaults.standard.bool(forKey: “isDarkModeKey”)
if isDarkModeEnabled {
mySwicth.setOn(true, animated : false)
//set the background to dark
} else {
mySwicth.setOn(false, animated : false)
//set the background to white
}
#IBAction func addInformation(_ sender: UIBarButtonItem) {
// check rateHourly has value
if let editedRateHourly = rateHourly.text {
print("editedRateHourly condition is \(editedRateHourly)")
if editedRateHourly != ""{
print("not nil")
// check edit value is number?
let num = Int(editedRateHourly)
if num != nil {
print("is num")
// add to database
UserDefaults.standard.set(editedRateHourly, forKey: "\(findDate())")
UserDefaults.standard.synchronize()
// back to last viewController
navigationController?.popToRootViewController(animated: true)
}else{
print("not num")
print("error alert push!!")
popErrorAlert()
}
}else {
print("nil")
print("editedRateHourly condition is nil")
popErrorAlert()
}
}
}
#IBAction func cannelInformationPage(_ sender: UIBarButtonItem) {
navigationController?.popToRootViewController(animated: true)
}
I want to create a new simple edit page. It's two problem for me that when I finish edition if-else will check the condition is correct or not and then save the data popToRootViewControlle. When I finish edition I click on "addInformation" BarButtonItem and I get UI wrong. the Other wrong is when I click on editField but I don't enter any condition. And then I click on "cannelInformationPage" UIBarButtonItem. It also get wrong.
It's what I get wrong
wrong information
Because it returns Bool
_ = navigationController?.popToRootViewController(animated: true)
_ = self.navigationController?.popViewController(animated: true)
If you want to come back to last view controller than you can try above line, hope its work for you.
In my camera app, I'm thinking of supporting tap to focus and meter. I have found, as a user, that my finger sometimes accidentally touches the screen somewhere, and it focuses there, so I want to undo it. In my app, I'm thinking of adding a Reset Focus button, which would undo the tap to focus: it would tell iOS to focus whereever it thinks is best (as it was before the tap to focus).
Does iOS offer an API for this?
When the user taps at a point, I can assign to focusPointOfInterest and exposurePointOfInterest in AVCaptureDevice. But I don't see functions clearFocusPointOfInterest() and clearExposurePointOfInterest(). How do I do this?
You need to set the focus to .continousAutoFocus, the exposure to .continuousAutoExposure, and the focal point to CGPoint(x:0.5,y:0.5). The following focus and autofocus code works for me.
#IBAction private func doFocusAndExpose(_ gestureRecognizer: UITapGestureRecognizer) {
let devicePoint = previewView.videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: gestureRecognizer.location(in: gestureRecognizer.view))
focus(with: .autoFocus, exposureMode: .autoExpose, at: devicePoint)
}
#IBAction private func doAutofocus(_ gestureRecognizer: UITapGestureRecognizer) {
let devicePoint = CGPoint(x:0.5,y:0.5)
focus(with: .continuousAutoFocus, exposureMode: .continuousAutoExposure, at: devicePoint)
}
private func focus(with focusMode: AVCaptureDevice.FocusMode,
exposureMode: AVCaptureDevice.ExposureMode,
at devicePoint: CGPoint) {
sessionQueue.async {
let device = self.videoDeviceInput.device
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) {
device.focusPointOfInterest = devicePoint
device.focusMode = focusMode
}
if device.isExposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode) {
device.exposurePointOfInterest = devicePoint
device.exposureMode = exposureMode
}
device.unlockForConfiguration()
} catch {
print("Could not lock device for configuration: \(error)")
}
}
}
So I've got a MPMoviePlayerController playing a video in the background.
If I tell it to load a different video using remote control notifications it works just fine.
However if I tell it to play the video it doesn't play?
Has anyone else had this problem or found a solution?
Code snippet:
override func canBecomeFirstResponder() -> Bool {
return true
}
override func remoteControlReceivedWithEvent(event: UIEvent!) {
if (event.type == UIEventType.RemoteControl){
if (event.subtype.toRaw() == 100 || event.subtype.toRaw() == 101){
didPressPausePlay(self)
}else if(event.subtype.toRaw() == 104){
didPressNext(self)
}else if(event.subtype.toRaw() == 105){
didPressPrevious(self)
}
}
}
#IBAction func didPressPrevious(sender: AnyObject) {
videoTitle.text = ""
if (currentIndex != 0){
currentIndex--
currentVideo = parsedVideoIds[currentIndex] as NSString
videoPlayerViewController = XCDYouTubeVideoPlayerViewController(videoIdentifier: currentVideo);
videoPlayerViewController.moviePlayer.backgroundPlaybackEnabled = true;
videoPlayerViewController.presentInView(self.view);
videoPlayerViewController.moviePlayer.controlStyle = MPMovieControlStyle.None
self.view.bringSubviewToFront(customControls);
videoPlayerViewController.moviePlayer.play()
currentImage = 0
pauseplayButton.setImage(pauseImage, forState: UIControlState.Normal)
}
}
I left out the rest because it all does the same thing.