Rather than creating two UIImageViews
, it seems logical to simply change the image
of one view. If I do that, is there anyway of having a fade/cross dissolve between the two images rather than an instant switch?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
Yes what you say is absolutely correct and thats the way to do it. I wrote this method & always use this to Fade in my image. I deal with
CALayer
for this. You need to import Core Animation for this.You could do the opposite for Fade out an image. After it fades out. You just remove it from superview (which is
UIImageView
).[imageView removeFromSuperview]
.Edit: there is a better solution from @algal below.
Another way to do this is by using predefined CAAnimation transitions:
See the View Transitions example project from Apple: https://developer.apple.com/library/content/samplecode/ViewTransitions/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007411
This is I think the shortest way of doing it. Create a UIView animation and commit it on your imageView.
I needed the transition to repeat indefinitely. It took a LOT of trial and error for this one but I finally got the end-result I was looking for. These are code snippets for adding image animation to a UIImageView in a UITableViewCell.
Here is the relevant code:
You could also package the fade-in feature in a subclass, so that you can then use it as a common UIImageView, as in the following example:
A possible implementation follows.
Note: the way you actually implement the fade-in in the
setImage:
function can change, and could be one of the other excellent examples described in the other answers to this question — creating an additional on-the-flyUIImageView
as I'm doing here might be an unacceptable overhead in your specific situation.IMMFadeImageView.h :
IMMFadeImageView.m :
The above code relies on a few assumptions (including ARC being enabled in your XCode project), is only intended as a proof of concept, and in the interest of clarity and focus, it stays relevant by omitting important unrelated code. Please don't just copy-paste it blindly.
By using the
highlightedImage
property this can be made a bit more simple. Here's an example in Swift 3. First set both normal and highlighted image:And when you want to change between those animated: