簡體   English   中英

如何響應IBAction在Swift中調整圖像大小?

[英]How do I resize an image in Swift in response to an IBAction?

我在Xcode中使用了下面的Swift代碼,並且嘗試對sizeSelected函數進行編程,以使在UISegmentedControl對象上更改值時,我的pizzaIcon.png圖像的尺寸會稍大(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 @IBOutlet weak var myImageView: UIImageView!

根據您是否使用約束條件,有幾種“調整”大小的方法,但有以下示例:

// 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之間切換,因此本機大小為100x100 / 200x200 / 300x300的pizzaIcon.png可能會很好,並讓UIImageView使用Scale to Fill進行縮放。 如果這看起來還不夠好,那么您還需要在調整框架大小的同時將.image交換為適當大小的pizzaIcon.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM