Swift: how to create FDWaveformview Dynamically - ios

I want to show wave of multiple audios in scrollview using swift4 and xcode 9. I am using cocoapos library FDWaveFormView for showing wave of audio file. For this I have to create fdwaveformview dynamically. Fdwaveformview works fine if I create this in story board. But it shows an error when create dynamically in swift class.
Code:
for index in selectedAudios {
audioQueue.append(AVPlayerItem(url: index as! URL))
print("aduio url: \(index)")
let waveForm = FDWaveformView(frame: CGRect(x: 0, y: 0, width: 300, height: 150)) // error
audio_scroll_view.addSubview(waveForm!)
}
Screen Shot Of Error
Error: FDWaveformView initializer is inaccessible due to 'internal' protection level

Solution:
let frame = CGRect(x: 0, y: 0, width: 300, height: 100)
let waveform = FDWaveformView()
waveform.frame = frame
audio_scroll_view.addSubview(waveform)

Related

Play Youtube video on ARImageAnchor in Swift

I want to play a youtube video on my image tracking module
like when image tracked then play youtube video on it.
Can anyone help me?
private func makeVideo(with url: URL, size: CGSize) -> SCNNode? {
DispatchQueue.main.async {
self.player.frame = CGRect(x: 0, y: 0, width: 650, height: 400)
self.player.autoplay = true
self.player.loadPlayer()
}
// 4
let avMaterial = SCNMaterial()
avMaterial.diffuse.contents = player
// 5
let videoPlane = SCNPlane(width: size.width, height: size.height)
videoPlane.materials = [avMaterial]
// 6
let videoNode = SCNNode(geometry: videoPlane)
videoNode.eulerAngles.x = -.pi / 2
return videoNode
}
import YoutubeKit
let player = YTSwiftyPlayer(playerVars: [.videoID("GJQsT-h0FTU")])
I try this but it shows a black screen on image but audio is play
this may be not exactly solution to your issue, but I hope it can help you in some ways
So, what you can do in addition to the Image Detection is to display the UIWebView
'UIWebView' was deprecated in iOS 12.0: No longer supported; please adopt WKWebView.
Even though the Xcode will warn you with something like the message above, but the WKWebView does not display a thing, so you will need to stick with WebView until they fix the issue
So, assuming that you have figured out the image detection part and you are able to locate and put nodes over detected image you can implement following function:
func displayWebSite(on rootNode: SCNNode, horizontalOffset: CGFloat) {
DispatchQueue.main.async {
// Open YouTube
let request = URLRequest(url: URL(string: "https://youtu.be/7ehEPsrw1X8")!)
// Define the size
let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 650, height: 900))
webView.loadRequest(request)
// Set size
let webViewPlane = SCNPlane(width: horizontalOffset, height: horizontalOffset * 1.45)
webViewPlane.cornerRadius = 0.025
// Define geometry
let webViewNode = SCNNode(geometry: webViewPlane)
// Set the WebView as a material to the plane
webViewNode.geometry?.firstMaterial?.diffuse.contents = webView
webViewNode.opacity = 0
// Put a little in front to avoid merger with detected image
webViewNode.position.z += 0.04
// Add the node
rootNode.addChildNode(webViewNode)
}
}
For your reference you can check one of my projects to see how that function works within a project

iOS UI Test: Wait for exist fails for dynamically added button

My application does a SDK auto update and initialization when it launches. When it completed I am adding a button to check if auto update and initialization are completed in my UI test:
let hiddenButton = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
hiddenButton.setTitle("databaseUpdated", for: .normal)
hiddenButton.accessibilityIdentifier = "databaseUpdated"
self.view.addSubview(hiddenButton)
I have tried following lines to check it but it always fails:
XCTAssertTrue(app.descendants(matching: .button)["databaseUpdated"].waitForExistence(timeout: 60))
XCTAssertTrue(app.children(matching: .button)["databaseUpdated"].waitForExistence(timeout: 60))
XCTAssertTrue(app.otherElements["databaseUpdated"].waitForExistence(timeout: 60))
XCTAssertTrue(app.buttons["databaseUpdated"].waitForExistence(timeout: 60))
I have confirmed that my button is in view hierarchy using Debug View Hierarchy:
UPDATE
I have decided to add the button in viewDidLoad with different identifier and update it when SDK is initialized:
private let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
override public func viewDidLoad() {
super.viewDidLoad()
button.isAccessibilityElement = true
button.accessibilityTraits = .button
button.accessibilityIdentifier = "initial"
view.addSubview(button)
}
when SDK is initialized it is set to:
button.accessibilityIdentifier = "databaseUpdated"
and here is what I see when I inspect the button in view debugger:
still test is failing to find it.
UPDATE 2
I am able to find the button using Accessibility Inspector:
Check if the ViewController that you add the button is visible when you run the query. If not your query will fail.

How to display color picker on button click, and on select of particular color i want to get a hex code of that color?

which is the best cocoapod of color pickets for ios application?
I am trying to implement DRColorPickerWheelView by installing cocoapods in my sample application, next I want to get the RGB value. How to get that - Please do help.
I am implementing as following,
correct me if I am wrong.
import UIKit
import DRColorPicker
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let img = UIImageView()
img.image = DRColorPickerImage("colormap")
img.frame = CGRect(x: 20, y: 90, width: 330, height: 300)
view.addSubview(img)
let v = DRColorPickerWheelView()
v.frame = CGRect(x: 10, y: 40, width: 350, height: 400)
view.addSubview(v)
// Do any additional setup after loading the view, typically from a nib.
}
}
try below code:
let str = yourSelectedColorVariable.hexStringFromColor
This category defined at: DRColorPicker

How can I make my app permanently display volume instead of ringer?

How can I make my app permanently display volume instead of ringer while the user is in my app. Currently when using the application the app displays the ringer sound adjuster.
You can use MPVolumeView .
let volumeViewHolder = UIView(frame: CGRect(x: 20, y: 100, width: view.bounds.size.width - 40, height: 200))
volumeViewHolder.backgroundColor = UIColor.gray
let volumeView = MPVolumeView(frame: volumeViewHolder.bounds)
volumeView.showsRouteButton = true
volumeView.showsVolumeSlider = true
volumeViewHolder.addSubview(volumeView)
view.addSubview(volumeViewHolder)
Its just a demo. You can have your version with frames and styling as per your requirements

Creating & adding buttons to scrollview dynamically

I have the following code that creates buttons based on the fetched data. After buttons are created they get added to a scrollview.
And they should display horizontally.
I can confirm that there is data but it shows weird things in simulator (see attached screenshot). What is wrong in my implementation?
override func viewDidLoad() {
super.viewDidLoad()
fetchRecipeCategory()
for var i = 0; i < self.category.count; i++ {
let frame1 = CGRect(x: 20, y: 20 + (index * 45), width: 45, height: 45 )
let button = UIButton(frame: frame1)
button.setTitle("\(category[i].name)", forState: .Normal)
button.backgroundColor = UIColor.blackColor()
self.categoryScrollView.addSubview(button)
}
print(category[0].name)
}
You need to use different y frame value for each button. Your button creation is using index for its y frame and you did not increment it.
Since your are using for loop, you could use i value for it.
let frame1 = CGRect(x: 20, y: 20 + (i * 45), width: 45, height: 45 )
this code will create vertical list of button. If you want to list your button horizontally, change x frame value for each button
let frame1 = CGRect(x: 20 + (i * 45), y: 20, width: 45, height: 45 )
As for your screenshot, it looks like that the button title is set to Optional("data"). if you really sure that your data source contains data, you could simply unwrap it for your button title. Better if you use optional binding for it.

Resources