Xamarin.iOS启动图片过渡及广告展示

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<bool> actLoaded;
		private NSTimer cutDownTimer;	// 接口加载计时
		private bool IsLoaded;			// 

		private const float LoadingTime = 7;
		private double changeLauchTime = 5;

		public LaunchingView (UIWindow _window,Action<bool> _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;
		}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值