[Xamarin]UITableViewCell九宫格图片点击全屏效果

    
        /// 
        /// 给TableViewCell中的图片添加全屏的手势
        /// 
        /// Image.
        /// Table view.
        /// Index path.
        /// Add to view.
        public static void AddFullScreenTap (this UIImageView image, UITableView tableView, NSIndexPath indexPath, UIView addToView)
        {
            image.UserInteractionEnabled = true;
            var oldRect = image.Frame;

            image.AddGestureRecognizer (new UITapGestureRecognizer (() => {

                var cell = tableView.CellAt (NSIndexPath.FromRowSection (indexPath.Row, indexPath.Section));
                var rect = tableView.ConvertRectToView (tableView.RectForRowAtIndexPath (indexPath), tableView.Superview);

                nfloat l = rect.X, t = rect.Y;
                var imgRect = image.GetRectWithSuperview (cell);
                l += imgRect.Left;
                t += imgRect.Top;

                UIView v = new UIView ();
                v.BackgroundColor = XYUtils.FromRGBA (0x00000055);
                addToView.AddSubview (v);
                v.MakeConstraints (addToView, (ns) => {
                    ns.Left.EqualTo (addToView);
                    ns.Right.EqualTo (addToView);
                    ns.Top.EqualTo (addToView);
                    ns.Bottom.EqualTo (addToView);
                });
                var tbRect = tableView.GetRectWithSuperview (addToView);

                UIImageView img = new UIImageView (
                    new CGRect (l, t + tbRect.Top, image.Frame.Width, image.Frame.Height)
                );
                v.AddSubview (img);

                img.Image = image.Image;
                image.Image = null;

                UIView.Animate (0.3, () => {
                    v.BackgroundColor = XYUtils.FromRGBA (0x000000ff);
                    var max = img.Image.Size.Width > img.Image.Size.Height ? UIScreen.MainScreen.Bounds.Width : UIScreen.MainScreen.Bounds.Height;
                    var size = img.Image.Size.GetScaleSize(max);
                    img.Frame = new CGRect (0, 0, size.Width, size.Height);
                    img.Center = addToView.Center;
                });

                v.AddGestureRecognizer (new UITapGestureRecognizer (() => {
                    UIView.Animate (0.3, () => {
                        v.BackgroundColor = XYUtils.FromRGBA (0x00000000);
                        img.Frame = new CGRect (l, t + tbRect.Top, image.Frame.Width, image.Frame.Height);
                    }, () => {
                        image.Image = img.Image;
                        v.RemoveFromSuperview ();
                    });
                }));
            }));
        }

        public static CGRect GetRectWithSuperview (this UIView view, UIView v)
        {
            nfloat l, r, t, b;
            l = r = t = b = 0;

            for (UIView i = view; i != v; i = i.Superview) {
                l += i.Frame.Left;
                r += i.Frame.Right;
                t += i.Frame.Top;
                b += i.Frame.Bottom;
            }

            return CGRect.FromLTRB (l, t, r, b);
        }

        public static CGSize GetScaleSize (this CGSize size, nfloat max)
        {
            bool isScal;
            return GetScaleSize (size, max, out isScal);
        }

        public static CGSize GetScaleSize (this CGSize size, nfloat max, out bool isScale)
        {
            isScale = true;
            var w = size.Width;
            var h = size.Height;

            var i = 0;
            if (w > max && h > max) {
                if (w > h) {
                    i = 1;
                } else {
                    i = 2;
                }
            } else if (w > max) {
                i = 1;
            } else if (h > max) {
                i = 2;
            } else {
                isScale = false;
                return size;
            }

            nfloat ww = 0;
            nfloat hh = 0;

            if (i == 1) {
                ww = max;
                hh = h * max / w;
            } else if (i == 2) {
                hh = max;
                ww = w * max / h;
            }
            return new CGSize (ww, hh);
        }

UIView的的扩展方法MakeConstraints实现在另一篇文章里:
http://www.jianshu.com/p/6d004f465a4e

001.gif

你可能感兴趣的:([Xamarin]UITableViewCell九宫格图片点击全屏效果)