Add a Wiggle
This will be a continuation of Multiple Objects and UITouch
If you do a long touch in the main section of the iPhone on a icon you will notice the icons will wiggle. This shows the user they can be deleted or moved around. So lets try to mimic that as best we can and when we stop a drag of our image it will wiggle for two seconds.
The first thing we need to do is import a framework to our project. So right click on the Frameworks tab. Then click on Add Framework. Then click QuartzCore FramwWork.
So first open up touchViewController.h and we need to add in two function
-(void)doWiggle:(UIView *)touchView; -(void)endWiggle:(NSTimer*)timer;
Now open up touchViewController.m and we need to add in two functions
-(void)doWiggle:(UIView *)touchView {
// grabbing the layer of the tocuhed view.
CALayer *touchedLayer = [touchView layer];
// here is an example wiggle
CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:@"transform"];
wiggle.duration = 0.1;
wiggle.repeatCount = 1e100f;
wiggle.autoreverses = YES;
wiggle.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(touchedLayer.transform,0.1, 0.0 ,1.0 ,2.0)];
// doing the wiggle
[touchedLayer addAnimation:wiggle forKey:@"wiggle"];
// setting a timer to remove the layer
NSTimer *wiggleTimer = [NSTimer scheduledTimerWithTimeInterval:(2) target:self selector:@selector(endWiggle:) userInfo:touchedLayer repeats:NO];
}
-(void)endWiggle:(NSTimer*)timer {
// stopping the wiggle now
[((CALayer*)timer.userInfo) removeAllAnimations];
}
Now that we have two functions. We need add the touchesEnded method that gets called when you lift your finger off the screen.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == image) {
[self doWiggle:[touch view]];
}
else if ([touch view] == image2) {
[self doWiggle:[touch view]];
}
}
Now your image will wiggle when you end your drag.
You can grab the source code here.
Multiple Objects and UITouch
Here is a bit of a continuation of Working with UITouch
Please follow that tutorial first. We will be using the source code from that as our starting point. So once you have the source code in Xcode open up touchViewController.h and we need to add in one new outlet for our second image.
So we want the file to look like this. I have commented the line I have added in.
#import <UIKit/UIKit.h>
@interface touchViewController : UIViewController {
IBOutlet UIImageView *image;
IBOutlet UIImageView *image2; // added this
}
@end
Now we need to open up IB and create the new object and make the connection. Double click on touchViewController.xib. So my view now looks like this

My connections now look like this

Also we need to set something for the views in order for a click and drag to work on them. We need to make sure user interaction is enabled on the two UIImageViews we have in our main view. So the property for one looks like the following. Make sure you set this for both image views
Now you can save your view in IB and load up touchViewController.m. In our previous example we had the following method.
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
// get the touch location
CGPoint touchLocation = [touch locationInView:touch.view];
// move the image view
image.center = touchLocation;
}
You can now either comment out that function or delete it. We don’t want to use it anymore. Instead add in the following method.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == image) {
// move the image view
image.center = touchLocation;
}
else if ([touch view] == image2) {
// move the image view
image2.center = touchLocation;
}
}
That function will now allow you to click on a imageview and hold the click and drag the image around the screen.
As always you can grab the source code here.
Access the Address Book
I got a request on how do I access the address book in the iPhone. So I took around 30 minutes to learn how and here is how we will do it.
First create a new View-Based Application and call it addressBook.
Now the first thing we want to do is setup the 4 IBOutlets and the 1 IBAction for our project. We also need to include the headers for the address book framework. So open up addressBookViewController.h and you want to make it look like the following.
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface addressBookViewController : UIViewController { IBOutlet UIButton *button; IBOutlet UILabel *firstName; IBOutlet UILabel *lastName; IBOutlet UILabel *number; } -(IBAction)getContact; @end
Now that they are added we need to add in the framework files. So you will right click on the addressBook in the Targets option on the left panel and click on Get Info.
At the bottom there is a + and click that and you need to add the following items in AddressBook.framework and AddressBookUI.framework
Now lets layout our view. So double click on addressBookViewController.xib. You want your view to look like the following.
I used the —–’s just so you can see there is a UILabel there. Now we need to setup the connections for the 4 outlets and the one action. You want it to look like the following

So now lets open up addressBookViewController.m and we want to add in the following methods.
-(IBAction)getContact {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// setting the number
/*
this function will set the first number it finds
if you do not set a number for a contact it will probably
crash
*/
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
number.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
// remove the controller
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
Now we can click build and go and see the following

As always you can grab the source here.
Working with UITouch
So here is a quick post on how to work with a UITouch. This tutorial will put a UIImageView on a view and then when you touch around the image view will be placed where you touch.
So lets start out by creating a new project and and use a View Based application as shown below.
Now I called the project touch.
So lets create an outlet for the image view we will create. So open up touchViewController.h. Add your outlet.
IBOutlet UIImageView *image;
Now take a image and drag it into your project. For this I will use the following image.

Now double click on touchViewController.xib and it will load IB. Take a UIImageView and drag it on the view. You also want to set the image to the image you dragged to your project. Your view should now look something like this

Now create the link from the outlet you created to the view so it looks like this in the File’s Owner.

Now save the file and quit. If you clicked build and go now we will just see a box on the screen and it does nothing. So we need to add in a function to handle the touch.
So open up touchViewController.m and add in the following code.
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
// get the touch location
CGPoint touchLocation = [touch locationInView:touch.view];
// move the image view
image.center = touchLocation;
}
So pretty much we are grabbing a touch even on any object it may touch. Then we are getting the location of the touch. We will then take that and move the image.
The thing that sent me for a loop is the locationInView accepts a UIView so I was wondering why self.view didn’t work. I still have no clue but touch includes the view that was touched and that seems to work nicely.
The Apple code works off sub-classing your view which I have never done but this seems to work nicely.
As always here is the source code.
Change background of a UITableViewCell
I was asked this question by a co-worker on how to do this. My first answer was when you init the cell just set the backgroundColor but that did not work at all.
If you followed my other tutorials and have a custom UITableViewCell open that file and in the initWithFrame method you want the following
MyContentView.backgroundColor = [UIColor redColor];







