我正在寻找一种简单的方法来从shuffledWord1中删除tilesColored String"ment"中的4个字符.
var word1: String = "employment"
var shuffledWord1: String = "melpyoemtn"
var tilesColored: String = "ment"
var characters = Array(tilesColored) // gives ["m","e","n","t"]
let newWord1 = word1.StringByReplacingOccurencesOfString("\(characters[0])", withString:"") // gives "elpyoetn"
Run Code Online (Sandbox Code Playgroud)
stringByReplacingOccurencesOfString只允许检查1个字符并删除BOTH m,所以我如何检查所有4个并且只删除每个的一个实例以返回"melpyo"?
提前感谢您提供任何帮助
我有一个UITableView在UIViewController并增加从代码,而不是IB编辑按钮.这附带UITableViewControllers但不是UIVC.如何在swift中使用此按钮将表格视图置于编辑模式?在此先感谢您的帮助.
class WordsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我想检查已连接到手机的AVAudio portTypes.下面的代码适用于BluetoothA2DP和耳机,但是当我将手机连接到我的汽车免提装置时,它不适用于BluetoothHFP.谁能帮我?!我认为已经通过免提/ AV /蓝牙以及许多其他方面的所有SO帖子,但无法解决为什么它不能识别蓝牙输出端口类型.
import AVFoundation
func startCheckAVConnection() {
// set the AVAudioSession to allow bluetooth. This do/try/catch doesn't seem to make a difference if it is here or not.
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
} catch{
print(error)
}
// Check possible outputs for handsfree
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
if outputs.count != 0 {
for output in outputs {
if output.portType == AVAudioSessionPortBluetoothA2DP {
peripheralLabel.text = "connected to BluetoothA2DP"
} else if output.portType == AVAudioSessionPortBluetoothHFP { // NOT RECOGNISED
peripheralLabel.text …Run Code Online (Sandbox Code Playgroud) 我有一个在NSObject类中的代码中创建的UI按钮,它控制UIViewController类中的游戏.该按钮在游戏的大部分时间内都能正常工作,但在某个时刻,我希望按钮能够淡出/淡出.一旦fadein/out开始动画,按钮就不再是交互式的.我已经设置了.AllowUserInteraction选项,但仍然没有运气.我很难过这个,所以任何帮助都非常感谢!
Class GameController: NSObject {
var gameView: UIView!
var nextButton: UIButton!
func gameViewSetUp() {
// create the nextButton
let imageNextButton = UIImage(named: "rightArrow") as UIImage?
self.nextButton = UIButton(type: .Custom)
nextButton.frame = CGRectMake(ScreenWidth-100,ScreenHeight-250,60,60)
nextButton.setTitle("", forState: UIControlState.Normal)
nextButton.setImage(imageNextButton, forState: .Normal)
nextButton.contentMode = .ScaleAspectFill
nextButton.backgroundColor = UIColor.clearColor()
nextButton.layer.cornerRadius = 5.0
nextButton.layer.borderWidth = 2.5
nextButton.layer.borderColor = UIColor.clearColor().CGColor
nextButton.addTarget(self, action: "nextPressed", forControlEvents: .TouchUpInside)
nextButton.userInteractionEnabled = true
gameView.addSubview(nextButton)
func playStageX() {
nextButtonWink()
}
}
func nextButtonWink() {
UIView.animateWithDuration(1.5, delay: 0, options: [.AllowUserInteraction, .Repeat, .CurveEaseInOut, .Autoreverse],
animations: …Run Code Online (Sandbox Code Playgroud) 是否有最新的快速解决方案来使UITextField占位符消失,或者一旦用户单击该字段(而不是当他们开始键入)时将光标移到最前面。我尝试了旧文章中的一些解决方案,并尝试了下面的代码,但似乎没有达到预期的效果。任何帮助,不胜感激。
class LogInViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var userTextField: UITextField!
// the placeholder text is set in IB, with clear Button set to Appears while editing, and clear when editing begins button checked.
func textFieldDidBeginEditing(textField: UITextField) {
userTextField.placeholder = nil
passwordTextField.placeholder = nil
}
}
Run Code Online (Sandbox Code Playgroud)
这为我工作:
添加userTextField.delegate = self以查看是否已加载。//谢谢,我错过了
func textFieldDidBeginEditing(textField: UITextField) {
if userTextField.isFirstResponder() == true {
userTextField.placeholder = nil
}
}
Run Code Online (Sandbox Code Playgroud) 这似乎已经在swift和objc中被问了几次,但是我看不到swift的正确答案,所以希望这次有人可以帮助我.我创建了一个自定义配件视图按钮,但需要正确的按钮操作:因为"accessoryButtonTapped"是一个无法识别的选择器.
调用tableViewCellDelegate方法需要什么选择器willSelectRowAtIndexPath?
我的代码是:
let cellAudioButton = UIButton(type: .Custom)
cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
cellAudioButton.addTarget(self, action: "accessoryButtonTapped", forControlEvents: .TouchUpInside) //INCORRECT ACTION:
cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
cellAudioButton.contentMode = .ScaleAspectFit
cell.accessoryView = cellAudioButton as UIView
Run Code Online (Sandbox Code Playgroud)
提前感谢任何可以提供帮助的人.
我正在使用UIBezierPath arc方法创建一个弧.我想要将圆弧的两端修圆 - 请参阅附图,了解具体要求!circularRect版本中使用的cornerRadius选项不适用于arc方法.任何人都有关于如何实现这一目标的想法?提前致谢.(这与先前提出的问题不同,因为它提供了确切的要求)
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
let radius: CGFloat = max(bounds.width, bounds.height)
let arcWidth: CGFloat = 10
let startAngle: CGFloat = 4.6 / 3 * ?
let endAngle: CGFloat = 4.4 / 3 * ?
let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = arcWidth
// all thats needed to make the ends rounded is
path.lineCapStyle = .Round
path.stroke()
Run Code Online (Sandbox Code Playgroud)
我有一个UIViewController我想将背景设置为一个图像.我想在代码中执行此操作IB,因此我可以随后更改图像.
阅读了有关.ScaleAspectFill我的图像的所有提示仍然没有调整大小以适应屏幕.有人可以提供任何建议吗?我的代码是:
override func viewDidLoad() {
super.viewDidLoad()
// set the inital backgroundColor
self.view.backgroundColor = UIColor(patternImage: gameBackgroundOne!)
self.view.contentMode = UIViewContentMode.ScaleAspectFill //doesnt seem to do anything!
self.view.clipsToBounds = true // is this needed?
self.view.center = view.center // is this needed?
}
Run Code Online (Sandbox Code Playgroud) swift ×8
ios ×4
uitableview ×2
animation ×1
background ×1
bluetooth ×1
string ×1
uibezierpath ×1
uibutton ×1
uitextfield ×1