gpt4 book ai didi

ios - 如何在 Swift 中调整图像大小以响应 IBAction?

转载 作者:行者123 更新时间:2023-11-30 12:35:10 25 4
gpt4 key购买 nike

我在 Xcode 中有下面的 Swift 代码,我正在尝试对 sizeSelected 函数进行编程,以便当 上的值发生更改时,我的 PizzaIcon.png 图像的尺寸变得稍大(90 x 90 像素) UISegmentedControl 对象。 PizzaIcon.png 图像通过名为 sizeSelected()IBAction 连接到 viewController

我阅读了所有关于此的堆栈溢出,但我无法得到任何可行的建议。

如果我将其粘贴到代码底部,什么函数/方法会起作用,以及如何从下面的 sizeSelected() 函数中调用图像大小调整函数?

import UIKit

class SecondViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

var crustSelected = "NY-Style"
var orderType = "Takeout"
let pickerViewArray = ["NY-Style","Stuffed","Chicago-Style","Neapolitan"];
var sauceChoice = "whole"
var pizzaSize = "small"
var order1Text = Bool()
var order2Text = Bool()



func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerViewArray[row]
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerViewArray.count
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
crustSelected = pickerViewArray[row]
}


@IBOutlet weak var orderTypeToggle: UISegmentedControl!
@IBOutlet weak var myPickerView: UIPickerView!
@IBOutlet weak var sauceChoiceToggle: UISegmentedControl!
@IBOutlet weak var sizeChoiceControl: UISegmentedControl!

//I created this image outlet by connecting the image to the viewcontroller
@IBOutlet weak var myImageView: UIImageView!

@IBAction func indexChanged(sender: UISegmentedControl) {

switch sauceChoiceToggle.selectedSegmentIndex
{
case 0:
sauceChoice = "the whole";
case 1:
sauceChoice = "the left-side of the";
case 2:
sauceChoice = "the right-side of the";
case 3:
sauceChoice = "none of the";
default:
break;
}

}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : ThirdViewController = segue.destinationViewController as ThirdViewController



if (DestViewController.order1Text == "" ) {
DestViewController.order1Text = "\(pizzaSize) \(crustSelected) crust pizza, sauce on \(sauceChoice) pizza"
} else {
DestViewController.order2Text = "\(pizzaSize) \(crustSelected) crust pizza, sauce on \(sauceChoice) pizza"
}



}


@IBAction func sizeSelected() {

//I want to call the image resizing method here

}


@IBAction func SubmitOrderClicked(sender: AnyObject) {
orderType = orderTypeToggle.titleForSegmentAtIndex(orderTypeToggle.selectedSegmentIndex)!

pizzaSize = sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)!

var alertView:UIAlertView = UIAlertView()
alertView.title = "Order Submitted"
alertView.message = "You have successfully submitted a \(orderType) order for a \(pizzaSize) \(crustSelected) crust pizza, with sauce placed on \(sauceChoice) pizza."
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()

}



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

好的 - 我猜 pizzaIcon.png 已经被设置为 @IBOutlet weak var myImageView: UIImageView!< 中的 .image/p>

“调整大小”的几种方法,具体取决于您是否使用约束,但示例:

// if NOT using constraints

let rct = myImageVIew.frame

if rct.width == 85 {

myImageVIew.frame = rct.insetBy(dx: 15, dy: 15)

} else {

myImageVIew.frame = rct.insetBy(dx: -15, dy: -15)

}

// assuming myImageView has a 1:1 ratio constraint, and
// connected to myImageView's height constraint

@IBOutlet weak var myImageViewHeightConstraint: NSLayoutConstraint!

// if YES using constraints

if myImageViewHeightConstraint.constant == 85 {

myImageViewHeightConstraint.constant = 100

} else {

myImageViewHeightConstraint.constant = 85

}

由于您只在 85x85 和 100x100 之间切换,因此您可能会同意 pizzaIcon.png 的原始大小为 100x100/200x200/300x300,并让 UIImageView 使用 Scale to Fill 处理缩放。如果这看起来不够好,那么您还需要在调整框架大小的同时将 .image 交换为适当大小的 pizzaIcon.png

关于ios - 如何在 Swift 中调整图像大小以响应 IBAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42970346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com