I want to write a (only one) method or category or something else about an Animation,
so that when I am setting any UIImageView's image property, the animation will be executed automatically.
any idea? Thanks.
You can subclass UIImageview.
Add a animationProperty.
Create a animation block.
Call animation block from the setMethod of the animationProperty.
As image is a property, you can use KVO.
First In the category add image-observer, and write your own animation method.
Related
In iOS, if UILabel inherits from UIView then why isn't the animate method available to UILabel and how would I have known to go up the class? I know its probably private to the UIView class but wouldn't it make sense to have it directly available to UILabel as well? How does Apple or developers dictate what methods should or should not be available?
Here's an example:
func animateLabelTransitions(){
UIView.animate(withDuration: 3.5, animations: {self.questionLabel.alpha = 1})
}
The animate methods are available to UILabel. They are class methods of UIView which means they are class methods of any subclass of UIView as well.
While pointless, you could have written your code as:
func animateLabelTransitions(){
UILabel.animate(withDuration: 3.5, animations {
self.questionLabel.alpha = 1
})
}
But what's the point? This would confuse a lot of people. Just call the animate methods on UIView.
Just because you call animate on UIView doesn't mean it restricts what you can put inside the animation or completion blocks.
animate is a class method, not an instance method; it applies to all views simultaneously, not some specific view. There's no need to specify which one thing you're trying to animate. You can put many things in the block and animate them all together. What would you expect to happen if you called UILabel.animate(...) and animated a button inside of the block? That would be totally legal (since it would just call the UIView version which can animate anything), but very confusing.
I made a sub class TKButton of UIButton and provided a property hasUpdate of type BOOL.
I tried to overwrite its setHasUpdate method and initialized the new view which is not configured frame while having red color.
When hasUpdate is Yes, make the hidden = No else Yes.
At last I used setNeedsLayout method. In this method layoutSubvew, tried to change the new view frame.
If I set hasUpdate = YES , I hope the _badgeView appeared. However,it seems that it does not appear immediately. It might be the setNeedsLayout is asynchronies.
Does anyone has the idea about this problem?
Thanks.
I would like to change my UIImageView I used as a background image on my parentViewController after my childView is loaded
in my viewDidLoad, I have this line, that pretty works :
self.parentViewController?.view.addSubview(myBackgroundImage)
I wish to change it while I got an event on app. But when I try again to change it by using the same line as above it doesn't work.
Anyone have a solution ?
I think it is a bad practice to access the view from another controller. I will suggest you to create separate method for this. Or may be just use IBoutlet for this, for example:
self.parentViewController?.myBackgroundImageView.image = UIImage("your_image")
I'm trying to get a handle on what's happening in my app...
I have a UICollectionViewCell subclass which has a UIImageView as a property.
In that subclass's init method, I programatically add the imageView property as a subView of the classes contentView:
[self.contentView addSubview:self.imageView];
At the point this happens, the 'self.imageView' property is null as the imageView has not been created and assigned to the property yet.
Later in my cellForItemAtIndexPath method, I assign the imageView property of the cell equal to an imageView object.
At this point I expect that the content view would now have a valid imageView subclass, but that's not what i'm seeing.
So my question is: Why doesn't it work?
Should I be able to add my 'null' property imageView as a subView of my self.contentView and then later assign the imageView property equal to an imageView object, and then see that object as one of the contentViews subviews?
I hope this question makes sense, it was a pain to describe!
Adding a subview which is nil sounds wrong to me
I think you are using a wrong approach. You should instantiate and add your imageView in your cellĀ“s init method and then, in the cellForItemAtIndexPath of your controller assign the image like this
cell.imageView.image = the image you want...
No, you shouldn't (and you see it doesn't work). A property is a reference to an instance of something. If that instance doesn't exist yet then you can't work with it. Trying to isn't setting up some rule for future use, it's just quietly doing nothing.
You can add a method like configureImageView: and call that instead. The implementation would set the property and add the image view as a subview.
(iPhone SDK 3.x:) I have a UIControl subclass that creates a different number of subviews depending on the length of an NSArray property. Please take my word for it that this needs to be a UIControl rather than a UIView.
Currently I implement subview management in drawRect, beginning by removing all subviews and then creating the appropriate number based on the property. I don't think this is very good memory management and I'm not sure if drawRect is really the appropriate place to add subviews. Any thoughts on the best way to handle this pattern?
Thank you.
There is a method called layoutSubviews, and like the name already says, that method is thought to layout the subviews. You can call setNeedsLayout and the layoutSubviews method will be called (do not call layoutSubviews directly).