1.实现效果
启动应用时,启动图片自然过渡到广告图片中(外加基本动画【波纹及上翻页】),如:
2.实现方式
在FinishedLaunching执行时,往UIWindow里AddSubview视图,在LaunchingView视图中通过模拟添加启动图片来实现自然过渡,从而达到显示广告的效果。
3.代码实现
1)控件实例
public class LaunchingView:UIView
{
private UIActivityIndicatorView actView;
private UIImageView defauleView;
private UIImage imgDefault;
private UIImage webImage;
private Action actLoaded;
private NSTimer cutDownTimer; // 接口加载计时
private bool IsLoaded; //
private const float LoadingTime = 7;
private double changeLauchTime = 5;
public LaunchingView (UIWindow _window,Action _actLoaded)
{
this.actLoaded = _actLoaded;
this.Frame = _window.Frame;
if (_window.Frame.Height > 480) {
imgDefault = UIImage.FromFile("Images/Default-568h.png");
} else {
imgDefault = UIImage.FromFile("Images/Default.png");
}
defauleView = new UIImageView (_window.Frame);
defauleView.Image = imgDefault;
this.AddSubviews (defauleView);
actView = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.Gray);
actView.Frame = new RectangleF ((this.Frame.Width-30)/2,(this.Frame.Height-30)/2,30,30);
this.AddSubview (actView);
this.CutDownTime ();
}
2)计时器,控制图片加载时间
#region 接口计时
public void CutDownTime()
{
this.LoadADViewData ();
//实时接口超时处理,自动跳转
int count = 0;
cutDownTimer = NSTimer.CreateRepeatingScheduledTimer (1,()=>{
count++;
if(IsLoaded){
this.Invalidate();
return;
}
if(count == LoadingTime){
this.Invalidate();
this.TheAnimation();
}
});
}
private void Invalidate()
{
if(cutDownTimer != null){
cutDownTimer.Invalidate();
cutDownTimer.Dispose();
}
}
#endregion
3)图片加载处理
#region 启动图片处理
private void LoadADViewData()
{
var imageUrl = "http://f.hiphotos.baidu.com/image/pic/item/9358d109b3de9c82bc5dfb826f81800a19d84386.jpg";
this.ShowTimeSetting (imageUrl);
}
private void ShowTimeSetting(string imageUrl)
{
if(!string.IsNullOrEmpty(imageUrl)){
actView.StartAnimating ();
var picUrl = NSUrl.FromString(imageUrl);
var manager = SDWebImageManager.SharedManager;
manager.Download (picUrl,SDWebImageOptions.ProgressiveDownload,ProgessHandler,CompletedHandler);
}
}
void ProgessHandler(uint cuSize,long total)
{
//下载图片进度
}
void CompletedHandler (UIImage image, NSError error, SDImageCacheType cacheType,bool finshed)
{
//下载完成
this.Invalidate();
actView.StopAnimating ();
this.IsLoaded = true;
if(image != null){
webImage = image;
}
this.TheAnimation ();
}
#endregion
4)动画过渡控制
#region 动画过渡
private void TheAnimation ()
{
if (webImage == null) {
//当没有节日图片时,设置画面停留时间短暂
changeLauchTime = 0;
}
CATransition animation = CATransition.CreateAnimation ();
animation.Duration = 1.5f;
animation.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.EaseInEaseOut);
animation.Type = "rippleEffect";
if (webImage != null) {
this.defauleView.Image = webImage;
}
this.Layer.AddAnimation (animation, "animation");
PerformSelector (new Selector ("ToUpSide"), null, changeLauchTime);
}
[Export ("ToUpSide")]
private void ToUpSide ()
{
//翻页动画
this.actLoaded (true);
UIView.BeginAnimations ("ChangeView");
UIView.SetAnimationDuration (1.0f);
UIView.SetAnimationCurve (UIViewAnimationCurve.EaseInOut);
UIView.SetAnimationTransition (UIViewAnimationTransition.CurlUp, this, true);
UIView.SetAnimationDelegate (this);
UIView.SetAnimationWillStartSelector (new Selector("WillStart"));
UIView.SetAnimationDidStopSelector (new Selector("DidMove"));
UIView.CommitAnimations ();
}
[Export("DidMove")]
private void DidMovew()
{
this.RemoveFromSuperview ();
}
[Export("WillStart")]
private void WillStart()
{
defauleView.RemoveFromSuperview ();
}
#endregion
5)调用
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// window = new UIWindow (UIScreen.MainScreen.Bounds);
// window.RootViewController = new RootViewController();
//// window.
var launchView = new LaunchingView (Window, (result) => {
});
Window.MakeKeyAndVisible ();
Window.AddSubview (launchView);
// launchView.CutDownTime();
return true;
}