Skip to content

Instantly share code, notes, and snippets.

@aimore
Last active August 9, 2017 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aimore/2b95a8f561e09402c2610394c251c122 to your computer and use it in GitHub Desktop.
Save aimore/2b95a8f561e09402c2610394c251c122 to your computer and use it in GitHub Desktop.
using UIKit;
using CoreGraphics;
using Foundation;
namespace Coverflow
{
public class CoverflowCell : UICollectionViewCell
{
private readonly UIImageView _iconImageView;
private readonly UIImageView _backgroundImageView;
public UIImage Image {
set {
_backgroundImageView.Image = new UIImage();
_backgroundImageView.SizeThatFits(new System.Drawing.SizeF());
_iconImageView.Image = value;
}
}
[Export("initWithFrame:")]
public CoverflowCell (CGRect frame) : base(frame)
{
var imageFrame = new CGRect (CGPoint.Empty, new CGSize (frame.Size.Width, frame.Size.Width)); // Not a typo - forcing square cell using width for both dimensions
_backgroundImageView = new UIImageView(imageFrame);
ContentView.AddSubview (_backgroundImageView);
_iconImageView = new UIImageView (imageFrame);
_iconImageView.Transform = CGAffineTransform.MakeScale (0.9f, 0.9f);
ContentView.AddSubview (_iconImageView);
}
}
}
using System;
using UIKit;
using CoreGraphics;
using CoreAnimation;
namespace Coverflow
{
public class CoverFlowLayout : UICollectionViewFlowLayout
{
private const int ACTIVE_DISTANCE = 200;
private const float ZOOM_FACTOR = 0.3f;
public CoverFlowLayout ()
{
ItemSize = new CoreGraphics.CGSize (105.0f, 105.0f);
ScrollDirection = UICollectionViewScrollDirection.Horizontal;
MinimumLineSpacing = 22.0f;
}
public override bool ShouldInvalidateLayoutForBoundsChange (CoreGraphics.CGRect newBounds)
{
return true;
}
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect (CoreGraphics.CGRect rect)
{
var array = base.LayoutAttributesForElementsInRect (rect);
var visibleRect = new CGRect (CollectionView.ContentOffset, CollectionView.Bounds.Size);
foreach (var attribute in array) {
if (attribute.Frame.IntersectsWith(rect)) {
float distance = (float)visibleRect.GetMidX () - (float)attribute.Center.X;
float normalisedDistance = distance / ACTIVE_DISTANCE;
if (Math.Abs(distance) < ACTIVE_DISTANCE) {
float zoom = 1 + ZOOM_FACTOR * (1 - Math.Abs (normalisedDistance));
attribute.Transform3D = CATransform3D.MakeScale (zoom, zoom, 1.0f);
attribute.ZIndex = 1;
}
}
}
return array;
}
public override CGPoint TargetContentOffset (CGPoint proposedContentOffset, CGPoint scrollingVelocity)
{
float offsetAdjustment = float.MaxValue;
float horizontalCenter = (float)(proposedContentOffset.X + (this.CollectionView.Bounds.Size.Width / 2.0));
var targetRect = new CGRect (proposedContentOffset.X, 0.0f, CollectionView.Bounds.Size.Width, CollectionView.Bounds.Size.Height);
var array = base.LayoutAttributesForElementsInRect (targetRect);
foreach (var layoutAttributes in array) {
float itemHorizontalCenter = (float)layoutAttributes.Center.X;
if (Math.Abs(itemHorizontalCenter - horizontalCenter) < Math.Abs(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
return new CGPoint (proposedContentOffset.X + offsetAdjustment, proposedContentOffset.Y);
}
}
}
using System;
using UIKit;
using CoreGraphics;
using Foundation;
namespace Coverflow
{
public partial class ViewController : UIViewController, IUICollectionViewDataSource
{
private UICollectionView _coverFlow;
private static readonly NSString cellToken = new NSString("CoverflowCell");
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
_coverFlow = new UICollectionView (new CGRect (new CGPoint (0, 0), new CGSize (View.Frame.Width, 200)), new CoverFlowLayout ());
_coverFlow.RegisterClassForCell (typeof(CoverflowCell), cellToken);
_coverFlow.WeakDataSource = this;
_coverFlow.BackgroundColor = UIColor.Clear;
_coverFlow.ShowsHorizontalScrollIndicator = false;
View.AddSubview (_coverFlow);
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
#region IUICollectionViewDataSource
public nint GetItemsCount (UICollectionView collectionView, nint section)
{
return 10; // Will be the count of items in a ViewModel?
}
public UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath)
{
CoverflowCell cell = (CoverflowCell)collectionView.DequeueReusableCell (cellToken, indexPath);
CoverflowCell cell2 = (CoverflowCell)collectionView.DequeueReusableCell(cellToken, indexPath);
cell.Image = new UIImage("page-0.jpg");
//cell2.Image = new UIImage("page-1.jpg");
//cell.Image.Size(size);
return cell;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment