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



我在 Xcode 中有下面的 Swift 代码,我正在尝试对 sizeSelected 函数进行编程,以使我的 pizzaIcon .png图像的尺寸在更改UISegmentedControl对象上的值时稍微变大(90 x 90 像素(。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

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

// 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 使用缩放到填充来处理缩放。如果这看起来不够好,那么您还需要在调整框架大小的同时将.image交换为适当大小的pizzaIcon.png

最新更新