Paper for Facebook image scroll in Xamarin.iOS

By | Blog

Paper for Facebook has some nice and neat features. One of them is scroll of an image using the tilt of the device.

Here is the implementation of such feature in Xamarin.iOS.

public override void ViewDidLoad ()
{
	base.ViewDidLoad ();
	
	// Perform any additional setup after loading the view, typically from a nib.
	mainScrollView.Bounces = false;
	mainScrollView.UserInteractionEnabled = false;

	var movingImageView = new UIImageView (UIImage.FromFile ("landscape.jpg"));
	mainScrollView.AddSubview (movingImageView);

	mainScrollView.ContentSize = new SizeF (movingImageView.Frame.Width, mainScrollView.Frame.Height);
	mainScrollView.ContentOffset = new PointF ((float)(mainScrollView.ContentSize.Width - View.Frame.Width) / 2.0f, 0);

	motionManager = new CMMotionManager ();
	motionManager.GyroUpdateInterval = 1 / 60.0;

	// this is how fast the image should move when rotate the device
	var motionMovingRate = 4;
	var shakeThreshold = 0.1;

	//get the max and min offset x value
	var maxXOffset = mainScrollView.ContentSize.Width - mainScrollView.Frame.Width;
	var minXOffset = 0;

	motionManager.StartGyroUpdates (NSOperationQueue.CurrentQueue, (CMGyroData gyroData, NSError error) => {
		if (Math.Abs (gyroData.RotationRate.y) >= shakeThreshold) 
		{
			var targetX = mainScrollView.ContentOffset.X - gyroData.RotationRate.y * motionMovingRate;

			if (targetX > maxXOffset)
						targetX = maxXOffset;
			else if (targetX < minXOffset)
						targetX = minXOffset;

			mainScrollView.ContentOffset = new PointF ((float)targetX, 0);
		}
	});
}

Download the source code and project from here.

Happy coding!

IOS6 style Navigation Bar in IOS7

By | Blog

So you need to create that old style look in an app, maybe because it is a legacy code and your client needs this because of approved style guides and new ones do not exist for IOS7 yet?

Then do not despair, it is easier than you think. I used appearance for UINavigationBar to set up the basics.

[[UINavigationBar appearance] setTitleTextAttributes: @{ UITextAttributeFont : SOME_FONT_IF_YOU_WISH }];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.clipsToBounds = YES;
    }

Then you need to make sure all the views are not offset by 64pixels down. It is again very simple, just force the inset to be used for non transparent navigation bar too. I call this in viewDidLoad and viewWillAppear.

- (void)updateInsetIOS7
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeAll;
        self.extendedLayoutIncludesOpaqueBars = YES;
    }
}

Happy coding!

Official London Guide featured by Apple as Editor’s choice!

By | Blog

Absolutely amazing news and cannot express how exited I’m, application we worked very hard for our client has been picked as Editor’s choice just at the moment when Olympics ceremony to be started. Download while it is hot!

http://itunes.apple.com/gb/app/london-official-city-guide/id536603270?mt=8

UrlImageStore concurrent threads limiting helper

By | Blog

Downloading images from Url is very common task in iOS apps. It is however important to limit the amount of threads being used as otherwise it gets pretty heavy on the CPU and whole device can be easily clogged up.

There is a nice example from Redth on github to use, however I had problems running it as it failed to add operation to NSOperationQueue for me.

So I rewrote it to use Task Parallels from .NET 4 and it works like a charm.

Here it is on GitHub, enjoy and happy coding!

https://github.com/sichy/UrlImageStore