I've been trying to get this snippit of code to work but I can't seem to get my UIButton to animate through images.
#IBOutlet weak var screenButton: UIButton!;
func animation(){
var animationButton : UIButton!;
var imageList = [UIImage]();
var image1:UIImage = UIImage(named:"3fingers-o-l-animation1.png")!;
var image2:UIImage = UIImage(named:"4fingers-o-l-animation1.png")!;
animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList;
animationButton!.imageView!.animationDuration = 1.0;
animationButton!.imageView!.startAnimating();
}
After writing this code I've dragged a line from screenButton to the button I want to animate through the image in the Storyboard.
What am I doing wrong?
What you need to do is to not re-initialize the UIButton in the function animation; so, comment out this line:
//var animationButton : UIButton!
Besides just having an IBOutlet, we need also an IBAction to your function animation. Do the same thing as you do for your button by control dragging a "line" to the block of function (check IBAction, not IBOutlet).
A little walk-through while you are still in storyboard:
What you have is only one button named: animationButton, which is an IBOutlet from your storyboard to your code by control-dragging a "line".
For your animation function:
Control-drag also from storyboard from the button a "line" to the block of the animation function.
Set up an initial image for you button in storyboard as will be mentioned shortly below.
In addition, we need to set up an initial image in storyboard; otherwise, the image property of the button is nil.
Please do so in storyboard by clicking on the right hand side "Attributes Inspector" and then set an image where it says "image". As test, I set that up as "1.jpg".
Thus, you code should look like the following:
#IBOutlet weak var animationButton: UIButton!
#IBAction func animation()
{
print("button touched")
//var animationButton : UIButton!
var imageList = [UIImage]()
var image1:UIImage = UIImage(named:"1.jpg")!
var image2:UIImage = UIImage(named:"2.jpg")!
//animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList
animationButton!.imageView!.animationDuration = 1.0
animationButton!.imageView!.startAnimating()
}
Just ran a quick test; it gets touch event and the images animate on the button. Hope this helps.
Related
I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?
Here the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageToSet = UIImage(named: "3-1")
b1.setBackgroundImage(imageToSet, for: .normal)
imageView.image = imageToSet
}
#IBOutlet weak var b1: UIButton!
#IBOutlet weak var imageView: UIImageView!
}
These are my setting for the button:
In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like
setBackgroundImage(..)
setImage(..)
This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default
So I am trying to give the user the ability to design their own virtual business card where the user can drag a label around the UIView, they can change background color of the UIView, as well as the color of the UILabel. The card itself is represented by a nib and the user selects the different color choices or template image choices through a UIPickerView. I want the Label to appear on top of the Image when it appears and is dragged to that part of the View, however it is not currently doing that. Changing colors of the background for the UIView and the text work just fine; I am also able to add and remove the Template image (which is a PNG) by using .isHidden().
My Problem is that when the UIImage is added to the UIView it is on top of the UILabel. If part of the UILabel overlaps with part of the png image it disappears behind it, and I would like it to appear over the image.
The code for the nib is in userCardView.swift and the picker view is on the mainViewController where I set the UserDefaults which works fine.
class userCardView: UIView {
#IBOutlet var cardView: UIView!
#IBOutlet weak var testLBL: UILabel!
#IBOutlet weak var templateImage: UIImageView!
var colorIndex = ["Black", "White", "Gray"]
var templateIndex = ["None", "Triangle"]
override func awakeFromNib() {
super.awakeFromNib()
cardView.clipToBounds = true
let backColorObject = UserDefaults.standard.object(forKey: "backGroundColor") as? String
let textColorObject = UserDefaults.standard.object(forKey: "textColor") as? String
let templateColorObject = UserDefaults.standard.object(forKey: "templateColor") as? String
let templateImageObject = UserDefaults.standard.object(forKey: "templateImage") as? String
for _ in templateIndex {
if templateImageObject == templateIndex[1] {
templateImage.isHidden = false
templateImage.image = UIImage(named: "test_triangle.png")
//testLBL.bringSubview(toFront: templateImage)
templateImage.bringSubview(toFront: testLBL)
} else if templateImageObject == templateIndex[0] {
templateImage.isHidden = true
} else {
templateImage.isHidden = true
}
}
I'm Still pretty new to using swift so any help at all would be greatly appreciated, thank you.
You need to ask testLBL's superview to bring it to the front. Your code templateImage.bringSubview(toFront: testLBL) won't work if templateImage is not the parent view of testLBL because testLBL is not in templateImage's view hierarchy.
So try to use the superview of testLBL
<testLBL's parent view>.bringSubview(toFront: testLBL)
or
testLBL.parentView.bringSubview(toFront: testLBL)
Hope it solves your problem
I have a textView and an imageView added in storyboard as shown in the screenshot below.
In story board order is:
textView
imageView
Now in run imageView covers textView and things assigned to textView does not work.
So I programmatically want to change the order to:
imageView
textView
(Without touching storyboard)
I tried:
self.view.bringSubviewToFront(textView)
self.view.sendSubviewToBack(imageView)
and
imageView.layer.zPosition = 1
textView.layer.zPosition = 2
None of these is working for me
I guess the problem is you might be using these codes in viewDidLoad method. Try using in viewDidAppear
Or another try is using performSelector
write this in viewDidLoad or viewWillAppear
self.performSelector(#selector(changeOrder), withObject: nil, afterDelay: 0.2)
and write your order changing in this function
func changeOrder() {
imageView.layer.zPosition = 1
textView.layer.zPosition = 2
}
Edit
I have made a sample. Have a look.
https://github.com/RajanMaheshwari/OrderingLayer
I have tried the code and its working fine
class ViewController: UIViewController {
#IBOutlet weak var myImageView: UIImageView!
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.performSelector(#selector(changeOrder), withObject: nil, afterDelay: 0.2)
}
func changeOrder() {
myImageView.layer.zPosition = 1
myTextField.layer.zPosition = 2
}
}
My Storyboard
My Final Outcome after running the code
Now imageView is on layer 1 and textField is on layer 2 position
If you comment out the performSelector line then the outcome will be
Here the imageView is on layer 2 position and textField is on layer 1 position.
There is three main points to understand the question:
1- There's a custom made class "Monster" that inherits from UIImageView superclass
2-There're two images(game characters) in the UI that user has to choose between them
3-According to this choice, one of the two images will appear and the other will be staying hidden
-Each of the images is connected to the UI with its own image
#IBOutlet weak var rockMonster: UIImageView!
#IBOutlet weak var fireMonster: UIImageView!
var activeCharacter: Monster!
-According to which button is pressed the Monster object will carry the result which differs in the image and the type of character
#IBAction func fireMonsterChoosed(sender: AnyObject) {
activeCharacter = Monster (typ: "fireMonster", frame: fireMonster.frame)
activeCharacter.hidden = false
#IBAction func rockMonsterchoosed(sender: AnyObject) {
activeCharacter = Monster (typ: "rockMonster", frame: rockMonster.frame)
activeCharacter.hidden = false
-Now, the activeCharacter var even after equalizing its .frame property with the one of the chosen character, it still has no control on the image so it's not connected with the UI
activeCharacter.hidden = false //No images appear
Q : I need to give activeCharacter var the control over these images in UI after the initialization I make in each IBAction.
Thanks in advance.
I'm new in programming and I'm stuck with this little problem. I created a table view with several items, that passes data to a label and an image view through a segue. It all works fine, but now I want to make the image clickable, in order to segue to another view controller to show this image expanded. How can I do that?
class ViewController: UIViewController {
#IBOutlet var titleLabel: UILabel!
#IBOutlet var descLabel: UILabel!
#IBOutlet var imageView: UIImageView!
var titleName: String?
var descName: String?
var imageName: String?
func configureView() {
if let poster = self.imageName {
if let imagePoster = self.imageView {
imagePoster.image = UIImage (named: poster)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = titleName
descLabel.text = descName
self.configureView()
}
The easiest thing to do is to use a UIButton instead of an image view. Set it's type to custom and install the image into the button in IB (Interface Builder).
That way you can trigger an IBAction just like any other button. It also highlights on a touch like you'd expect, triggers on touch up rather than touch down, etc.
If you don't want to use a button-with-image, you have to set userInteractionEnabled = YES on the image view and install a tap gesture recognizer on it. See the docs on UITapGestureRecognizer for more information.