swift3

Swift app's audio don't match the brightness intensity of the image section randomly?

∥☆過路亽.° 提交于 2021-01-29 12:02:28
问题 I have this application that takes an image (under OpenCVWrapper) that is grayscale and turns the brightness of that specific section that's touched by the Apple Pencil into sound. The dark areas don't make any sound, the medium areas make a medium pitch, and the bright areas make a high pitch. But for some reason the code works in some areas of the image flawlessly and in other it's almost reversed. Code Below: //Produce Sound Based on Location of Touch Input @objc func strokeAction(gesture:

Play song in Notification

本小妞迷上赌 提交于 2021-01-29 05:30:07
问题 I want to play a song. When a notification comes. My code is working fine when the app is foreground mode. I want to play a song when the app is in background mode. Please help. 回答1: Add custom sound to your local notification Swift 3.0 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { var state: UIApplicationState = application.applicationState if state == .active { var soundID: SystemSoundID var mainBundle: CFBundleRef =

how to find duplicate values in integer array by loop in swift?

风流意气都作罢 提交于 2021-01-28 21:11:14
问题 I need to know how to find duplicate values in integer array by loop method in swift? I tried to -> func findDuplicates (array: [Int]) { var prevItem = array[0] for i in 0...array.count-1 { if prevItem == array[i] { print(i) } else { print("there is no any duplicates values") } } } please show my solution in this way! 回答1: You can use a set and every time you try to insert an element it fails it means it is a duplicate. You would need also to make sure you don't keep duplicate elements on the

How to transpose a matrix of unequal array length in Swift 3

天涯浪子 提交于 2021-01-28 07:52:48
问题 My array is of type [[Int]] array = [[1,2,3], [4,5,6], [7,8,9,10], [11,12,13], [14,15,16]] I want the transpose result as: array = [[1,4,7,11,14], [2,5,8,12,15], [3,6,9,13,16], [0,0,10,0,0]] How to pad 0's to an array which does not have an equal row or column mapping. I want the transpose to work for both rows and columns with unequal mapping elements. Please help. 回答1: Here is an implementation that does what you want: func transpose(_ input: [[Int]]) -> [[Int]] { let columns = input.count

Swift 3 - WKWebView Load Local HTML not loading resource files

此生再无相见时 提交于 2021-01-28 07:16:06
问题 I was download my website from online to store in xcode project with a sub-folder and want to run in WKWebView locally. I found the webview loaded webpage miss all CSS/JS/Images resources. I search around the internet and still not working. Is webpage inside relative url incorrect or my code to get the Main Bundle location problem? Anyone can help. Thanks. Xcode Project Structure HTML Page Contents Sample I wrote this code to load WKWebView let htmlFile = Bundle.main.path(forResource:

Refresh JSON data after a few seconds in swift

回眸只為那壹抹淺笑 提交于 2021-01-28 06:30:46
问题 I want this to update every 4 seconds with fresh data form the url, but i dont know how to do this. This is what i have so far and it works fine but without the refresher! The Refresher needs to work like a youtube subscriber counter that update every 4 seconds or so. I have looked at a timer but i couldn't make it work because (i think) its a searchBarSearchButtonClicked function and the urlRequestid has to have a input! Please help! Thanks! func searchBarSearchButtonClicked(_ searchBar:

CAShapeLayer. Does the line pass through the point?

戏子无情 提交于 2021-01-28 05:12:29
问题 I use CAShapeLayer in order to draw a line on the screen. In the method touchesEnded I want to check " Does the line pass through the point?". In my code when I press on the any part of the screen the method contains returns always true. Perhaps, I have problem in line.frame = (view?.bounds)! . How can I fix it? Sorry for my bad English. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first let firstPosition = touch?.location(in: self) if

iOS - Tap Gesture issue on image view

心已入冬 提交于 2021-01-28 01:54:23
问题 I am slowly working my way through Apple's learn developing for Swift and I keep running into an issue with my tap gesture. I have re-created the project numbers times all with the same outcome. I add a tap gesture to an image view and this should open the photo library from my computer. Nothing happens. When I download and run the sample file from Apple everything works. When I copy and paste the code from Apple's to mine, again, nothing happens. I have gone through everything that I can but

How to change UIScrollView Indicator color?

萝らか妹 提交于 2021-01-27 21:06:25
问题 When I use UIColor.blue its changing, but when I try to apply RGB, its not reflected in scrollview indicator. func scrollViewDidScroll(_ scrollView: UIScrollView) { let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView) verticalIndicator.backgroundColor = UIColor(red: 211/255, green: 138/255, blue: 252/255, alpha: 1) // verticalIndicator.backgroundColor = UIColor.blue } 回答1: I think problem is here verticalIndicator.backgroundColor =

How to use DispatchGroup to make asynchronous calls within a for loop

扶醉桌前 提交于 2021-01-27 18:52:01
问题 In my example code below, I call complete(false) on failure. However, since I'm using a DispatchGroup object to make sure all asynchronous requests are complete, I cannot just call syncGroup.leave() on failure, as the notify will be called, which contains complete(true) , making this function return true , when it should be returning false for failure. Am I correct in not calling syncGroup.leave() on failure to complete my function correctly? Or should I be calling syncGroup.leave() and