Hello All,
I have an app, which is playing url mp3 audio with two image buttons - play and stop. Now I would like to improve it a little bit. I have two .png images (play.png and pause.png) right now and I would like them to change with each other with a tap depending on whether the stream is on or off. Any ideas how to make it? Here is my code:
import UIKit
import AVKit
import MediaPlayer
class ViewController: UIViewController, AVAudioPlayerDelegate {
var player : AVPlayer!
var dict = NSDictionary()
@IBAction func playButtonPressed(_ sender: UIButton){
let url = "https://stream.com/radio.mp3"
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: [])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
player = AVPlayer(url: URL(string: url)!)
player.volume = 1.0
player.rate = 1.0
player.play()
}
@IBAction func stopButtonStopped(sender: UIButton) {
player.pause()
}
Post
Replies
Boosts
Views
Activity
Hello,
I'm looking for help with annotationView. What I'm going to do is to have already added icons in annotations, but when Im going to tap the annotation (icon) I would like to see the button in annotation view frame, as in this:
After I click it I would like to see this white frame and uibutton:
I was looking for help in old discussion, but did notg found an answer: UIButton in AnnotationView - How to keep icon and button
Here is my viedidload:
super.viewDidLoad()
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "identifier") \\ important part
checkLocationServices()
znajdzSzczytyNaMapie(szczyty)
circles = szczyty.map {
MKCircle(center: $0.coordinate, radius: 100)
}
mapView.addOverlays(circles!)
mapView.delegate = self
}
Here in viewdidload I've added mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "identifier") to viewdidload to register indentifier.
Here is the rest of the code:
guard !(annotation is MKUserLocation) else { return nil }
//let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")
let identifier = "identifier"
//guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView else { return nil }
guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil }
let btn = UIButton(type: .detailDisclosure)
annotationView.rightCalloutAccessoryView = btn
switch annotation.title!! {
ase "Turbacz":
annotationView.markerTintColor = UIColor(red: 0.86, green: 0.99, blue: 0.79, alpha: 1.00)
annotationView.glyphImage = UIImage(named: "bald")
case "example":
annotationView.markerTintColor = UIColor(red: 0.80, green: 0.98, blue: 0.73, alpha: 1.00)
annotationView.glyphImage = UIImage(named: "bear")
default:
annotationView.markerTintColor = UIColor.green
annotationView.glyphImage = UIImage(named: "gora")
}
return annotationView
}
But Still I got this:
Hello,
I'm learning swift now, so this is my second question here :) I'm creating the app with some annotations on the map. I've added MKCircles in radius 200m from the annotation. This should be a radius in which user can check-in and save his loacation. Can I use distance(from:)to do it?
In general I'm looking for some tips/advices how to set this check in (Could be check-in button inside the annotation view) that saves user location if user is in radius 200m from the annotation. The check-in should be connected to the name and describtion of the annotation (sczyt.name and szczyt.opis), and the annotation name should be saved in a table view "Zdobyte Szczyty", which is another tab bar view in the app. My mainstoryboard looks like this:
There is a Map controller and Zdobyte Szczyty controller. so I would like to save location only if in radius 200m from annotation as annotation name (szczyt.name) and describtion (szczyt.opis) in table view, which is based in Zdobyte Szczyty viewcontroller.
Here is my map viewcontroller.swift:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate{
let locationManager = CLLocationManager()
struct Szczyt {
let name: String
let opis: String
let lattitude: CLLocationDegrees
let longtitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
.init(latitude: lattitude, longitude: longtitude)
}
}
@IBOutlet weak var mapView: MKMapView!
@IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .satellite
default:
mapView.mapType = .hybrid
}
}
private(set) var circles: [MKCircle]!
let szczyty = [Szczyt(name: “one”, opis: “describtion”, lattitude: 50.825061725039226, longtitude: 16.908595782487315),
Szczyt(name: “two, opis: “describtion”, lattitude: 50.223874478583854, longtitude: 20.996341184611302),
Szczyt(name: “three”, opis: “describtion”, lattitude: 50.756134079897516, longtitude: 15.984675411850157)]
override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
znajdzSzczytyNaMapie(szczyty)
circles = szczyty.map {
MKCircle(center: $0.coordinate, radius: 200)
}
mapView.addOverlays(circles!)
mapView.delegate = self
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
checkLocationAuthorization()
} else {
// Show alert letting the user know they have to turn this on.
}
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
case .denied: // Show alert telling users how to turn on permissions
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
case .restricted: // Show an alert letting them know what’s up
break
case .authorizedAlways:
break
}
}
func znajdzSzczytyNaMapie(_ szczyty: [Szczyt]) {
for szczyt in szczyty {
let annotations = MKPointAnnotation()
annotations.title = szczyt.name
annotations.subtitle = szczyt.opis
annotations.coordinate = CLLocationCoordinate2D(latitude:
szczyt.lattitude, longitude: szczyt.longtitude)
mapView.addAnnotation(annotations)
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")
switch annotation.title!! {
case “one”:
annotationView.markerTintColor = UIColor(red: 0.86, green: 0.99, blue: 0.79, alpha: 1.00)
annotationView.glyphImage = UIImage(named: "bald")
case “two”:
annotationView.markerTintColor = UIColor(red: 0.80, green: 0.98, blue: 0.73, alpha: 1.00)
annotationView.glyphImage = UIImage(named: "bear")
case “three”:
annotationView.markerTintColor = UIColor(red: 0.73, green: 0.98, blue: 0.68, alpha: 1.00)
annotationView.glyphImage = UIImage(named: "spruces")
}
return annotationView
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.green
circleRenderer.fillColor = UIColor.green
circleRenderer.alpha = 0.3
circleRenderer.lineWidth = 1.0
return circleRenderer
}
}
Here is my ZdobyteSzczyty view controller:
import UIKit
import SwiftUI
class Zdobyte_ViewController: UIViewController {
struct ContentView: View {
var body: some View {
Text("Test viewcontroller gór") //this is not working
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Hello All,
This is my first question here, so please be lenient ;)
I'm trying to add add circular (MKCircle) annotations for all of these model objects in my code instead of adding every of them with coordinations. Here is my code:
let locationManager = CLLocationManager()
struct Szczyt {
let name: String
let opis: String
let lattitude: CLLocationDegrees
let longtitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
.init(latitude: lattitude, longitude: longtitude)
// here Im trying to create var for this property to set it for all objects
}
}
@IBOutlet weak var mapView: MKMapView!
@IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .satellite
default:
mapView.mapType = .hybrid
}
}
let circles = szczyty.map {
MKCircle(center: $0.coordinate, radius: 100)
//here I got - Cannot use instance member 'szczyty' within property initializer; property initializers run before 'self' is available
}
let szczyty = [Szczyt(name: "one", describtion: "describtion one", lattitude: 50.333061725039226, longtitude: 16.708595782487315),
Szczyt(name: "Two", describtion: "Describtion two", lattitude: 50.444874478583854, longtitude: 20.896341184611302),
Szczyt(name: "Three", describtion: "Describiton three", lattitude: 50.555134079897516, longtitude: 15.884675411850157)]
Can someone can give me a tip or advice how to solve this? Once again - Im trying to set MKCircle for all of the objects in szczyty. Thank you in advance for your help.
In the next steps I would like to detect if user is in those circles by setting 'distance(from:)' method to determinate user location between circle and user location. Than user should have an option to check in in specified area. Also if you would have any tips and advices what to use to do it, I'd be very thankful.
Paweł