How can i zoom mask uiimage? - scroll

mask remove after zoommask imageI want to zoom the masked image.
I can display the mask image but when i try to zoom the image then mask is not working.
I want to zoom the image in mask area only.

Please check the code below. its working for me.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrolView: UIScrollView!
#IBOutlet weak var imgPhoto: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrolView.delegate = self
scrolView.minimumZoomScale = 1.0
scrolView.maximumZoomScale = 10.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imgPhoto
}
}

Related

CGColor not defined for the UIColor, need to first convert colorspace. Swift 2, Xcode 7

I was trying to change the color of my app´s buttons by creating the color using sliders to select the values of Red, Green, Blue and Alpha. So I created an variable which held the color created by the user.
ViewController is where the buttons are.
ChangeColors is the RGB Sliders System.
import UIKit
import Foundation
var buttonColor = UIColor()
class ViewController: UIViewController {
#IBOutlet var tools: UIButton!
#IBOutlet var custom: UIButton!
#IBOutlet var support: UIButton!
#IBOutlet var donate: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
tools.backgroundColor = buttonColor
custom.backgroundColor = buttonColor
support.backgroundColor = buttonColor
donate.backgroundColor = buttonColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The second is the code of te RGB Slider System.
import Foundation
import UIKit
class ChangeColors: UIViewController {
#IBOutlet var Red: UISlider!
#IBOutlet var Green: UISlider!
#IBOutlet var Blue: UISlider!
#IBOutlet var Alpha: UISlider!
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.
}
#IBAction func preview(sender: AnyObject) {
let rVal = CGFloat(Red.value)
let gVal = CGFloat(Green.value)
let bVal = CGFloat(Blue.value)
let aVal = CGFloat(Alpha.value)
self.view.backgroundColor = UIColor(red: rVal, green: gVal, blue: bVal, alpha: aVal)
}
#IBAction func change(sender: AnyObject) {
let rVal = CGFloat(Red.value)
let gVal = CGFloat(Green.value)
let bVal = CGFloat(Blue.value)
let aVal = CGFloat(Alpha.value)
let color = UIColor(red: rVal, green: gVal, blue: bVal, alpha: aVal)
buttonColor = color
}
}
But the app crashes as soon as it open and get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -CGColor not defined for the UIColor ; need to first convert colorspace.
I really need help. Thank You.
The problem is that an instance created by the UIColor() initialiser doesn't represent an actual color. If you look at the crash message in more detail, you'll see that it actually creates a UIPlaceholderColor instance – which (as the name suggests) appears to act as a 'placeholder' in the absence of any color information. Therefore you can't assign it to the backgroundColor of any of your views.
The fix is define a default color for your buttonColor. In your case I'd advise clearColor.
var buttonColor = UIColor.clearColor()

Swift Adding Image to a Button

I want to add an image to a button, and when it is clicked, i want it to change. I'm very new to Swift so I know very little.
I have it like this:
//
// ViewController.swift
// Tip Calculator
//
// Created by ios4 on 10/16/15.
// Copyright (c) 2015 ios4. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var tipAmount: UILabel!
#IBOutlet weak var userBillTextField: UITextField!
#IBOutlet weak var tenOutlet: UIButton!
override func viewDidLoad()
{
tipAmount.text = " "
// var billDouble = (userBillTextField.text as NSString).doubleValue
super.viewDidLoad()
}
#IBAction func calculateButton(sender: UIButton)
{
var billDouble = NSString(string: userBillTextField.text).doubleValue
tipAmount.text = String( stringInterpolationSegment: billDouble * 0.15)
}
#IBAction func tenButton(sender: UIButton)
{
}
and I want the outlet called ten outlet to change to a different image called 10_selected_image from 10_unselected_image.
Any help?
Here are my instructions for this : https://s3.amazonaws.com/mmhighschool/MasterDocs/TipCalculator/TipCalculatorAppDirections.pdf
I'm currently working on stretch 3
You should be able to use set image for control state in your button action
button.setImage(image: UIImage(named: <image name>), forState: UIControlState.Normal)
If you need more info about how to make a "toggle button" this thread has a good answer to that
How to use a UIButton as a toggle switch
For me in Swift 5 is working like:
boton1.setBackgroundImage(UIImage(named: "image Name"), for: UIControl.State.normal)

Swift 2.0 animating images

Trying animate series of images in swift 2.0 but following the video tutorials like https://www.youtube.com/watch?v=PSny6un3VUw the method that this guy uses are no longer relevant and swift 2.0 no longer allows arrays to be setup in such means. The basic code this guy used is as follows:
class ViewController: UIViewController: {
#IBOutlet var myImageView: UIImageView
#IBOutlet var animationBtn: UIButton
var imageList = UIImage[]() // no longer valid?
#IBAction func animationBtnClicked(sender: AnyObject) {
startAnimation
}
override func viewDidLoad() {
for i in 1...13
{
let imageName = "\(1)"
imageList += UIImage(named: imageName) //line no longer works
}
}
func startAnimation() -> Void
{
myImageView.animationImages = imageList
myImageView.startAnimation()
}
Anyone know a better way in swift 2.0 to animate images or anyway in general how to do this?
I hope I will help you
#IBOutlet var myImageView: UIImageView!
#IBOutlet var animationBtn: UIButton!
var imageList = [UIImage]()
imageList.append(UIImage(named: imageName))
or
imageList += [UIImage(named: imageName)]

Changing image of UIButton via click - XCode 6 Swift

Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin).
This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblOutput: UILabel!
#IBOutlet weak var lblWin: UILabel!
#IBOutlet weak var lblLost: UILabel!
#IBOutlet weak var lblWinsAmt: UILabel!
#IBOutlet weak var lblLossesAmt: UILabel!
let coin = UIImage(named: "penny_head") as UIImage;
override func viewDidLoad() {
super.viewDidLoad()
//imgShell1.hidden = true //doesnt work
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnStart(sender: UIButton) {
}
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
The way you're setting up the control is incorrect. Assuming you have a button property named btnShell (and it's the button you want to setup) change your viewDidLoad() method to:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
To permanently change the button image on tap, you have to use the .Normal enum case and not .Highlighted for the control state:
sender.setImage(coin,forState: UIControlState.Normal)
Setting the image for the .Highlighted state makes the new image appear only when the button is in that state, i.e. when it is tapped.
If you are looking to change the UIButton's background image permanently you have to use Antonio's method:
sender.setImage(coin,forState: UIControlState.Normal)
This won't change the UIKit's default highlighting when you tap the button. When you don't want the button to be highlighted when you touch it or have different appearances for different states, then you might be better off using an UIImageView.
In viewDidLoad():
imgView?.image = imgOne
imgView?.userInteractionEnabled = true
imgView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "changeImage:"))
The function that changes the image:
func changeImage(recognizer: UITapGestureRecognizer){
self.imgView?.image = imgTwo
}
You can also use isSelected.
button.setImage(image2, for: .normal)
button.setImage(image1, for: .selected)

How to use gesture to move large image in a view using swift

I can able to do pinch zoom using pinch gesture using swift. But after image enlarges, i can't able view the which is out of view. (i.e)can't able to move image around the screen. Can any one help me?
My code:
#IBAction func scaleImage(sender: UIPinchGestureRecognizer) {
sender.view!.transform = CGAffineTransformScale(sender.view!.transform, sender.scale, sender.scale)
sender.scale = 1
}
I need to view full image after zooming it.
Add ScrollView and add image on scrollView
class ViewController:UIScrollViewDelegate{
#IBOutlet var imageView: UIImageView!
#IBOutlet var scroll: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scroll.maximumZoomScale=4;
scroll.minimumZoomScale=1.0;
scroll.bounces=true;
scroll.bouncesZoom=true;
scroll.contentSize=CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
scroll.showsHorizontalScrollIndicator=true;
scroll.showsVerticalScrollIndicator=true;
scroll.delegate=self;//assigning delegate
self.scroll.addSubview(imageView);
self.view.addSubview(scroll);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZoomingInScrollView(_ scrollView: UIScrollView) -> UIView?
{
return self.imageView
}
}

Resources