playing a sound file in a pageControler with Xcode and Swift
I would like to play a sound file when a new page comes on the screen.
So far I did that to have the pageControler
working with an array of images.
Can someone tell me where I have to put my code in order to play an array of sound? So that the sound1
will be played when the image1
comes on the screen.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var images: [String] = ["1", "2", "3", "4", "5"]
var frame = CGRect(x:0,y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pageControl.numberOfPages = images.count
for index in 0..<images.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let imgView = UIImageView(frame: frame)
imgView.image = UIImage(named: images[index])
self.scrollView.addSubview(imgView)
}
scrollView.contentSize = CGSize(width:(scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
}
ios swift xcode audio
add a comment |
I would like to play a sound file when a new page comes on the screen.
So far I did that to have the pageControler
working with an array of images.
Can someone tell me where I have to put my code in order to play an array of sound? So that the sound1
will be played when the image1
comes on the screen.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var images: [String] = ["1", "2", "3", "4", "5"]
var frame = CGRect(x:0,y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pageControl.numberOfPages = images.count
for index in 0..<images.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let imgView = UIImageView(frame: frame)
imgView.image = UIImage(named: images[index])
self.scrollView.addSubview(imgView)
}
scrollView.contentSize = CGSize(width:(scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
}
ios swift xcode audio
add a comment |
I would like to play a sound file when a new page comes on the screen.
So far I did that to have the pageControler
working with an array of images.
Can someone tell me where I have to put my code in order to play an array of sound? So that the sound1
will be played when the image1
comes on the screen.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var images: [String] = ["1", "2", "3", "4", "5"]
var frame = CGRect(x:0,y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pageControl.numberOfPages = images.count
for index in 0..<images.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let imgView = UIImageView(frame: frame)
imgView.image = UIImage(named: images[index])
self.scrollView.addSubview(imgView)
}
scrollView.contentSize = CGSize(width:(scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
}
ios swift xcode audio
I would like to play a sound file when a new page comes on the screen.
So far I did that to have the pageControler
working with an array of images.
Can someone tell me where I have to put my code in order to play an array of sound? So that the sound1
will be played when the image1
comes on the screen.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var images: [String] = ["1", "2", "3", "4", "5"]
var frame = CGRect(x:0,y:0, width:0, height:0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pageControl.numberOfPages = images.count
for index in 0..<images.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let imgView = UIImageView(frame: frame)
imgView.image = UIImage(named: images[index])
self.scrollView.addSubview(imgView)
}
scrollView.contentSize = CGSize(width:(scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
}
ios swift xcode audio
ios swift xcode audio
edited Nov 19 at 4:55
kit
1,1083616
1,1083616
asked Nov 19 at 0:49
david andersson
356
356
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I don't see a direct way to get called when the selected page changes. You could add code in your page view controller's setViewControllers(_:direction:animated:completion:)
method that figures out which page is active and plays the appropriate sound.
If you use a page control you could probably also subclass UIPageControl and use a didSet on the currentPage
property to figure out which sound to play.
Edit:
Just add a new file, make it a Cocoa touch class, and make it a subclass of UIPageControl
. Name it CustomPageControl
. Then your implementation can be as simple as this:
import UIKit
class CustomPageControl: UIPageControl {
override var currentPage: Int {
didSet {
//Your code to play sounds based on selected index could go
//here, or broadcast a notification that your view controller
//would listen for
print("New page index = (currentPage)")
}
}
}
Then just select the page control on your UIPageViewController, select the "Identity Inspector", and change the class of the page control to your custom CustomPageControl
class. Once you've done that, whenever your page index changes, the didSet method above will be called.
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366985%2fplaying-a-sound-file-in-a-pagecontroler-with-xcode-and-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't see a direct way to get called when the selected page changes. You could add code in your page view controller's setViewControllers(_:direction:animated:completion:)
method that figures out which page is active and plays the appropriate sound.
If you use a page control you could probably also subclass UIPageControl and use a didSet on the currentPage
property to figure out which sound to play.
Edit:
Just add a new file, make it a Cocoa touch class, and make it a subclass of UIPageControl
. Name it CustomPageControl
. Then your implementation can be as simple as this:
import UIKit
class CustomPageControl: UIPageControl {
override var currentPage: Int {
didSet {
//Your code to play sounds based on selected index could go
//here, or broadcast a notification that your view controller
//would listen for
print("New page index = (currentPage)")
}
}
}
Then just select the page control on your UIPageViewController, select the "Identity Inspector", and change the class of the page control to your custom CustomPageControl
class. Once you've done that, whenever your page index changes, the didSet method above will be called.
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
|
show 6 more comments
I don't see a direct way to get called when the selected page changes. You could add code in your page view controller's setViewControllers(_:direction:animated:completion:)
method that figures out which page is active and plays the appropriate sound.
If you use a page control you could probably also subclass UIPageControl and use a didSet on the currentPage
property to figure out which sound to play.
Edit:
Just add a new file, make it a Cocoa touch class, and make it a subclass of UIPageControl
. Name it CustomPageControl
. Then your implementation can be as simple as this:
import UIKit
class CustomPageControl: UIPageControl {
override var currentPage: Int {
didSet {
//Your code to play sounds based on selected index could go
//here, or broadcast a notification that your view controller
//would listen for
print("New page index = (currentPage)")
}
}
}
Then just select the page control on your UIPageViewController, select the "Identity Inspector", and change the class of the page control to your custom CustomPageControl
class. Once you've done that, whenever your page index changes, the didSet method above will be called.
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
|
show 6 more comments
I don't see a direct way to get called when the selected page changes. You could add code in your page view controller's setViewControllers(_:direction:animated:completion:)
method that figures out which page is active and plays the appropriate sound.
If you use a page control you could probably also subclass UIPageControl and use a didSet on the currentPage
property to figure out which sound to play.
Edit:
Just add a new file, make it a Cocoa touch class, and make it a subclass of UIPageControl
. Name it CustomPageControl
. Then your implementation can be as simple as this:
import UIKit
class CustomPageControl: UIPageControl {
override var currentPage: Int {
didSet {
//Your code to play sounds based on selected index could go
//here, or broadcast a notification that your view controller
//would listen for
print("New page index = (currentPage)")
}
}
}
Then just select the page control on your UIPageViewController, select the "Identity Inspector", and change the class of the page control to your custom CustomPageControl
class. Once you've done that, whenever your page index changes, the didSet method above will be called.
I don't see a direct way to get called when the selected page changes. You could add code in your page view controller's setViewControllers(_:direction:animated:completion:)
method that figures out which page is active and plays the appropriate sound.
If you use a page control you could probably also subclass UIPageControl and use a didSet on the currentPage
property to figure out which sound to play.
Edit:
Just add a new file, make it a Cocoa touch class, and make it a subclass of UIPageControl
. Name it CustomPageControl
. Then your implementation can be as simple as this:
import UIKit
class CustomPageControl: UIPageControl {
override var currentPage: Int {
didSet {
//Your code to play sounds based on selected index could go
//here, or broadcast a notification that your view controller
//would listen for
print("New page index = (currentPage)")
}
}
}
Then just select the page control on your UIPageViewController, select the "Identity Inspector", and change the class of the page control to your custom CustomPageControl
class. Once you've done that, whenever your page index changes, the didSet method above will be called.
edited Nov 20 at 18:57
answered Nov 19 at 1:00
Duncan C
91.8k13114194
91.8k13114194
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
|
show 6 more comments
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
thx a lot! I will try but I don't know how to subclass UIPageControl. do you maybe have a link that can be useful for me? thank you very much for your time
– david andersson
Nov 20 at 18:34
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
No, I don't have a link. I pulled that idea out of my... hat. Yeah, my hat.
– Duncan C
Nov 20 at 18:40
thx. I will investigate
– david andersson
Nov 20 at 18:46
thx. I will investigate
– david andersson
Nov 20 at 18:46
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@davidandersson It just tried it and it works. I'll edit my answer.
– Duncan C
Nov 20 at 18:53
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
@Ducan C thx a lot! I will try! many thx
– david andersson
Nov 20 at 20:48
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366985%2fplaying-a-sound-file-in-a-pagecontroler-with-xcode-and-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown