Skip to main content

Posts

Showing posts from April, 2012

Unlocking the magic of the UIScrollView (Xcode/iOS)

The secret to understanding the scroll view is that it has a frame size and a content size. You only need to create the frame size big enough to fit on the screen whereas the content size will be larger in order for it to scroll.

Here is code that can be placed in the viewDidLoad of your ViewController.m file, and incorporates paging as well. It assumes that you have already created a UIScrollView in Interface Builder and called it scrollView:


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Adjust scroll view content size, set background colour and turn on paging
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 12,  
    scrollView.frame.size.height);
    scrollView.pagingEnabled=YES;
    scrollView.backgroundColor = [UIColor blackColor];

     // Generate content for our scroll view using the frame height and width as the reference
     point
    int i = 0;
    while (i<=11) {

  …

Synchronized surfing in iOS (Javascript, UIWebView and Objective-C video demo)

Following the last blog post on using Javascript as a means of communication between the contents of a UIWebView and the main thread of Objective-C in Xcode, I just wanted to post this proof of concept from the iOSSimulator.

Here two web views are placed side-by-side. When the left view scrolls down the page the right one follows, but when it goes back towards the top the right one stays where it is until it passes where the right one had stopped and then resumes.

The right web view can be scrolled entirely independently of the left one but then when the left view is scrolled again it syncs the position of the right one if it is at a point earlier in the article to the left one.

Watch the video to make sense of this.

If you'd like to know more then catch up with me on Twitter at @anthonylevings or @sketchytech

UIWebView, Javascript and Objective-C

This is a short blogpost explaining how to communicate between a UIWebView and your Objective-C code in Xcode (for iOS) using Javascript and a UIWebView instance method called:
stringByEvaluatingJavaScriptFromString:
Here's what you need to do:
(1) Create a New Project with a Storyboard (2) In the Storyboard drag out a UIWebView (3) Connect the UIWebView to your ViewController.h file and give it a name so that your .h file looks like this
// //  JavascriptViewController.h //  Javascript

#import
@interface JavascriptViewController : UIViewController @property (weak, nonatomic) IBOutletUIWebView *myWebView;
@end
(4) Next we need the ViewController class to follow the UIWebViewDelegate protocol so we add this after the superclass name so the ViewController.h file now looks like this:
// //  JavascriptViewController.h //  Javascript
#import
@interface JavascriptViewController : UIViewController <UIWebViewDelegate> @property (weak, nonatomic) IBOutletUIWebView *myWebView;
@end
(5) Mo…

Searching strings in Xcode using NSScanner and NSRegularExpression

In this post I discuss the different ways in which NSScanner and NSRegularExpression approach searching an NSString in Xcode, and how to make use of the results they provide.

For the entirety of this post and the examples contained in it let's presume we have the following string:


NSString* string = [NSStringstringWithFormat:@"You have %i chances to find me.", 9];

If we use NSScanner then we send the string we wish to search to the NSScanner instance that we are creating:


// First of all create the scanner using the string that you want to scan NSScanner* scanner = [NSScannerscannerWithString:string];

before using the instance methods to perform the searches:

// Scanning method to find an integer     // First we create an integer intnumberToFind; // next we find the number and send it to the integer     [scanner scanInt:&numberToFind]; // Scanning string up to a set of characters     //First we need a pointer to a string to send the result to     NSString* s; // next we send ou…

Making the switch to the Blogger dynamic template: Is it worth it??

For those of you who've been following this blog for a while, you'll know that I recently switched over to Blogger's dynamic template to make it easier for people to discover new and old articles alike.

I'm happy to report that it has proven itself to do exactly that. People stay on the site longer read more pages per visit and generally there is a better flow of traffic.

The switch happened at the end of March and as you can see from the graph the number of page views sharply increased.

If you rely on ads then the dynamic template isn't as flexible, but if your main aim is to disseminate information then I'd highly recommend.

for loops, for…in loops, random numbers (Xcode for iOS)

While the video isn't that exciting, in programming sometimes we want random things to happen to add texture and variety, and for this purpose loops are ideal. So here's a short piece of code that can be cut and pasted into your view controller .m file, and each time you run it what appears will be different in subtle ways:


// Method for creating random numbers between zero and the number sent  -(int)randomNumbers:(int)range {int a = arc4random_uniform(range+1); return a;}

- (void)viewDidLoad {     [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColorblackColor]; // Create a number of UIView objects using a for loop and random numbers to determine the size and position, giving them each a tag for identification later int i = 0;     for (i=0; i<=20; i++)     { int random = [selfrandomNumbers:10];  UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(10*i*random, 10*i*random, i*random/2, i*random/2)];      …

From SVG to Objective-C (Quartz 2D) with Qwarkee

It may come as a surprise that iOS doesn't natively support SVG images. You must therefore either use a TIFF, JPEG, PNG or GIF, or draw your images natively in code. It comes as a welcome addition then that Qwarkee has been created to avoid the need for adding multiple image files to your project to cover all the size and resolution bases of the iOS devices without creating every image manually in code.

The question is does it work? The answer is yes. And here's how:

(1) Open Qwarkee
(2) Drag your image to the drop zone
(3) Select a destination for your Objective-C .m and .h files
(4) Open your Xcode project
(5) Use ⌥ + ⌘ + A to add files to your project
(6) Select the two files you created
(7) With any UIView selected in your Storyboard change its class to that of your image
(8) Build and run

Here's an SVG image created in Inkpad on the iPad



and here's the same image in Quartz 2D running in the iPad simulator a few seconds later:

I'm still getting to grips with s…

The view with no name (Xcode for...in statement)

Let's suppose that you've dragged out a random number of UIView objects onto your View Controller in your app's Storyboard. Now we want to change their attributes but we don't necessarily want to bother giving each one a named pointer.

Here's what we'd do in the corresponding ViewController.m file. First we'd gather an array of subviews for the current view:

NSArray*subviews = [self.viewsubviews];

Next we'd do something like set all the backgrounds to blue with a for...in loop


for (UIView* v in subviews) {             v.backgroundColor=[UIColorblueColor];}; }
The v is an arbitrary variable name, and on each run through the loop it iterates to become one greater, just as if we'd written:
int i;
for (i=0; i<subviews.count; i++) {

     [[subviews objectAtIndex:i] setBackgroundColor:[UIColorblueColor]];};



The latter just takes longer to type and creates more code to read.
So here we have the whole thing in the viewDidLoad: method of our ViewController.m file:

NSArray, NSDictionary and retrieving objects in Xcode (iOS)

This is a very sketchy post but all is explained in the code example, which can be placed inside any ViewController.m file you choose to create.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *numbersEnglish = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", nil];
    NSArray *numbersFrench = [[NSArray alloc] initWithObjects:@"Un", @"Deux", @"Trois", @"Quatre", nil];
    NSArray *numbersItalian = [[NSArray alloc] initWithObjects:@"Uno", @"Due", @"Tre", @"Quattro", nil];

    // We've now created the arrays (containing NSStrings) that we'll be using in the dictionary

    NSDictionary *numberDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:numbersEnglish, @"English", numbersFrench, @"French", numbersItalian, @"Italian"…