Friday, 30 July 2010

Touch4Apps Blog | posts about iPhone : iPad : iPod app development

When setting up an image to the UIImageView for dbl resolution for retina display make sure to set the image after setting the frame of the view as initWithImage on UIImageView does not work properly.

 

1
2
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[backgroundImageView setImage:[UIImage imageNamed:@"bg-wall01.jpg"]];

 

Where in resources we have file named " This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", iOS4 does the trick for us to set the image scale to 2.0 for us (or rather 0.5 in fact).

Happy coding!

 

No more need to explain. How to get the current datetime and have it stored.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// get time and rotate minute arrow and hour arrow
// get current time
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 
// get the current hour & min
[dateFormatter setDateFormat:@"HHmm"];
NSDate *today = [NSDate date];
NSString *str = [dateFormatter stringFromDate:today];
NSString *strHour = [str substringToIndex:2];
NSString *strMin = [str substringFromIndex:2];
 
// convert hour and minute to the integer values so we can use it
NSInteger hour = [strHour integerValue];
NSInteger minute = [strMin integerValue];
 

 

 

Sometimes the image provided by the UIImagePicker is not the right size, and putting it to the UIImageView is not enough as we do need to know the size it was being fit to.

Let's cut the chase, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public static class UIImageUtils
{
///
///
///
///
/// A
///
public static UIImage ScaleToFit(UIImage sourceImage, System.Drawing.SizeF targetSize)
{
UIImage newImage;
 
SizeF imageSize = sourceImage.Size;
float width = imageSize.Width;
float height = imageSize.Height;
 
float targetWidth = targetSize.Width;
float targetHeight = targetSize.Height;
 
float scaleFactor = 0f;
float scaledWidth = targetWidth;
float scaledHeight = targetHeight;
 
PointF thumbnailPoint = new PointF(0, 0);
 
if (imageSize != targetSize)
{
float widthFactor = targetWidth / width;
float heightFactor = targetHeight / height;
 
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
 
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
 
// center the image
 
if (widthFactor < heightFactor)
{
thumbnailPoint.Y = (targetHeight - scaledHeight) * 0.5f;
}
else if (widthFactor > heightFactor)
{
thumbnailPoint.X = (targetWidth - scaledWidth) * 0.5f;
}
}
 
RectangleF thumbnailRect = new RectangleF(new PointF(0, 0), new SizeF(scaledWidth, scaledHeight));
 
UIGraphics.BeginImageContext(thumbnailRect.Size);
 
sourceImage.Draw(thumbnailRect);
 
newImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
 
return newImage;
}
}

Hope this helps.

Happy coding!

When implementing touch and gesture based interaction in your application, for example the swipe effect, you had to handle the TouchesMoved, TouchesBegan, TouchesEnded and TouchesCancelled events and handle the location of the finger and last location and so on, based on the quality of the algorithm it worked or not.

With the new SDK and with the great support from MonoTouch team, its current version of MonoTouch implements the UIGestureRecognizer and UIGestureRecognizerDelegate and related classes - UITapGestureRecognizer, UISwipeGestureRecognizer, UIRotationGestureRecognizer, UIPinchGestureRecognizer, UILongPressGestureRecognizer, UIPanGestureRecognizer. Lets have a look and few simple steps to take advantage of these common gesture implementations on the iPhone/iPad. For the demonstration we will use the swipe gesture.

First we need to have a view controller and define a Selector for the gesture recognizer. More on Selectors check the iPhone SDK, in general it is a special form of message. In MonoTouch Selectors can be consumed (fired from MonoTouch code) or exposed, being available to SDK functions and classes to be called from within.

Lets have a look at the typical implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
public partial class MyViewController : UIViewController
{
public static Selector MySelector
{
get
{
return new Selector("HandleSwipe");
}
}
///
/// and other class definition
///
}

Please note that the Selector is defined as a static access property returning the identification of the selector via its name (HandleSwipe).

Next we need to define the gesture recognizer and its delegate and assign it to the view. We do this in the ViewDidLoad event of the controller implementation class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// define the gesture recognizer 
MonoTouch.UIKit.UISwipeGestureRecognizer sgr = new MonoTouch.UIKit.UISwipeGestureRecognizer();
 
// add the target to it, we put the instance itself of the controller
// and the class instance selector
sgr.AddTarget(this, MainComicsViewController.MySelector);

// add the swipe direction, there are 4 of them (left, right, up, down). If other than one swipe is
// needed then more recognizers must be defined and added to the view - each for the direction
sgr.Direction = UISwipeGestureRecognizerDirection.Left; 

// also assign the delegate
sgr.Delegate = new SwipeRecognizerDelegate();
 
// and last, add the recognizer to this view to take actions
this.View.AddGestureRecognizer(sgr);

Then we need our delegate class for the swipe recognizer:

1
2
3
4
5
6
7
public class SwipeRecognizerDelegate : MonoTouch.UIKit.UIGestureRecognizerDelegate
{
public override bool ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
return true;
}
}

And last and finally our handler for the swipe event:

1
2
3
4
5
6
7
8
[Export("HandleSwipe")]
public void HandleSwipe(UISwipeGestureRecognizer recognizer)
{
// get the point of the swipe action
PointF point = recognizer.LocationInView(this.View);
 
// TODO: do something with the swipe
}

Well, that is all and we are done.

Happy coding!