問題描述
Làm thế nào để triển khai hoạt ảnh nhấp nháy trong iOS? (How to implement blink animation in iOS?)
I noticed interesting animation effect in iPhone application. Cell with current date is blinking/flashing when user activate application or touch Today button.
How to implement the same effect? Is it possible to implement it without image resources (for example use only CoreGraphics)?
Thanks
參考解法
方法 1:
Use UIAnimation
and animate the label's alpha property.
方法 2:
Use timer for continuous blinking effect
mytimer = [NSTimer scheduledTimerWithTimeInterval:0.06f
target:self
selector:
@selector(willStartBlinkingAnimation)
userInfo:nil
repeats:YES];
Write one function which will show/hide your date label. Or else you can set alpha over here, for even time set it to 0.5f, for odd time set it to 1.0f.
‑ (void) willStartBlinkingAnimation {
[UIView animateWithDuration:1.f
animations:^{
[currentDateLabel setHidden:![currentDateLabel isHidden]];
}];
}
When you want to stop this process, or at your viewDidUnload method, stop the process and release your timer:
[mytimer invalidate];
mytimer = nil;
Hope this will help you to fulfill your requirement.
方法 3:
I assume you mean that effect which 'shines' through the calendar day (by pressing Today).
1) Creating gradient.
Option A: To achieve this effect you should create a transparent png with the proper height of the table row. A sample img looks like this (it's only for illustration, pls create one):
Option B: create gradient using CAGradientLayer. Still you have to use an UIImageView to effectively animate the gradient on the cell's row. I would recommend Option A until you don't want to programmatically manipulate gradient visuals.
2) Placing the image on the row.
Option A: if you are sure that this animation always takes place on the top row then you can add the gradient image simply over the table.
Option B: (what the mentioned application might uses) you have to place the gradient image inside the cell. Maybe the best choice is to use custom cells (as the mentioned app uses, see the dates on the left in boxes) where you place your gradient image on the left side of the cell (placing X coord = ‑gradient_img.width).
3) Animating gradient.
Start the animation sequence on that action what you would like to link. Eg. I think the app uses the table view's scrollViewDidEndScrollingAnimation delegate method to trigger the animation.
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear
animations:^{
viewShiningImage.frame = CGRectMake(320, 0, viewShiningImage.frame.size.width, viewShiningImage.frame.size.height);
}
completion:^(BOOL finished) {
viewShiningImage.frame = CGRectMake(‑viewShiningImage.frame.size.width, 0, viewShiningImage.frame.size.width, viewShiningImage.frame.size.height);
}
];