Archive for 'Objective C'

How to Add Observer to a UI Class in Xcode

It is possible to add multiple observers to UI Class objects in Xcode. Let’s see how to add one to a UIControl object.

Consider a UIControl object, mySampleControl. Now we can add observer to this UIControl object as follows

[mySampleControl addObserver:self forKeyPath:@“my_value" options:0 context:NULL];

Here,

addObserver will receive a NSObject

KeyPath receives a NSString key

Options receives NSKeyValueObservingOptions

Context will receives the content

Now we have to write the method for the observer as follows :


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

if(object == mySampleControl && [keyPath isEqualToString:@"my_value"]) {

// Here is the observer that we have added for the object, mySampleControl and for the key path, my_value

}

}

This is how its done.

Calculating Angle to a Point in Xcode

Here is how we can calculate the angle to a point in relation to the center of a layer in Xcode.
// Point offset by the center


CGPoint centerOffset = CGPointMake(currentPoint.x - CGRectGetMidX(self.view.bounds), currentPoint.y - CGRectGetMidY(self.view.bounds));

CGFloat angleToPoint = atan2(centerOffset.y, centerOffset.x);

Here, currentPoint is the CGPoint object, the angle to which we are to determine. The CGFloat object, angleToPoint will contain the final result, which is the angle to the point currentPoint.

In Xcode, we can directly generate random values between two integers. But if we want to generate floating random values between two consecutive integers, say 0 and 1, here is what we can do.

CGFloat randomValue = (arc4random() % 101) / 100.f;

Similarly, if we want to generate floating random values between, 50 and 51, here is what we can do.

CGFloat randomValue = (arc4random() % 101) / 100.f + 50;

This is how we can do it.

How to Implement 3D Rotation in Xcode

In Xcode, it is possible to implement 3D rotation of layers using its transform property. The method that we use to implement this 3d rotation transformation is


CATransform3DMakeRotation(CGFloat angle, CGFloat x, CGFloat y, CGFloat z)

Parameters :
angle – parameter which determines the angle of rotation.

x – parameter which determines the extend of rotation to be done in the X axis

y – parameter which determines the extend of rotation to be done in the Y axis

z – parameter which determines the extend of rotation to be done in the Z axis

With the option to implement rotation based on all the 3 dimensional axis of a plane, we can implement single axis rotation and also multiple axis rotation, thereby making the rotation 3 dimensional.

If we only want to implement say only X axis rotation, then we can use like below :

layer.transform = CATransform3DMakeRotation(angle, 1, 0, 0);

Similarly, if we only want to implement say only Z axis rotation, then we can use like below :

layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);

Likewise, if we want to implement rotation on X and Z axis, then we can use like below :

layer.transform = CATransform3DMakeRotation(angle, 1, 0, 1);

So this is an extremely useful for creating 3D rotation for any object using native Xcode.

Create UISegmentedControl programatically

You can create an UISegmented control in a cocos2d or Xcode project with the help of following lines code:

// Create an UIView

    UIView *profileView = [[UIView alloc] init];
    profileView.frame = CGRectMake(0, 50, 320, 395);
    profileView.backgroundColor = [UIColor clearColor];
    [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

// Create the segmented control label array

    NSArray *itemArray = [NSArray arrayWithObjects: @"Credit", @"Debit", nil];

// Create the UISegmentedControl and add to the UIView

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] 
                                                      initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(5, 20, 150, 35);
    segmentedControl.center = CGPointMake(220, (50*5));
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    segmentedControl.selectedSegmentIndex = 1;
    segmentedControl.tintColor = [UIColor 
                         colorWithRed:0.1f green:0.4f  blue:0.65f  alpha:1.0f];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

    [segmentedControl addTarget:self
    action:@selector(segmentChanged:)
    forControlEvents:UIControlEventValueChanged];
   [profileView addSubview:segmentedControl];

Create UISwitch programatically

You can create an UIswitch in cocos2d or Xcode programatically with the help of following lines of code:

// Create an UIView

    UIView *profileView = [[UIView alloc] init];
    profileView.frame = CGRectMake(0, 50, 320, 395);
    profileView.backgroundColor = [UIColor clearColor];
    [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

// Create the switch and add to the UIView

    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:
                                           CGRectMake(130, 235, 0, 0)];
    [mySwitch addTarget:self action:@selector(changeSwitch:) 
                   forControlEvents:UIControlEventValueChanged];
    [profileView addSubview:mySwitch];

// Call function to get state

    -(void)changeSwitch:(UISwitch *)mySwitch{
        NSLog(@"%@", mySwitch.on ? @"On" : @"Off");
    }

We can use the NSTemporaryDirectory() method to get the temporary directory path of an IOS application. The output from this method will be the entire path of the temporary directory of the application, if the directory is there. If the temporary directory is not present, then this will return nil. The output will be in string format.

You can use the following lines of code to check whether an image is in the app bundle or not in Xcode :

-(void)CheckForImageInBundle
{
NSString *exists = [[NSBundle mainBundle]pathForResource:@"Image" ofType:@"png"];  //This stores the path of the resource with the name "Image" and of type "png" to the string. Nothing will be stored if there is no such file.
if(exists == nil){
NSLog(@"Doesn't exist");
}
else{
NSLog(@"Exists!");
}
}

Incoming search terms:

  • iOS verify that image is part of a bundle
  • html5 how to check if an image exists

To formally conform to a protocol:

class_addProtocol([MyViewController class], @protocol(SomeProtocol));

Even more dynamically:

class_addProtocol(objc_getClass(“MyViewController”), objc_getProtocol(“SomeProtocol”));

To actually add method implementations to a class:

// – (int)someMethod:(int)arg;

int someMethod(id self, SEL _cmd, int arg)

{

return arg * 2;

}

 

class_addMethod([MyViewController class], @selector(someMethod:), (IMP)someMethod, “i@:i”);

How to change sprite’s speed dynamically?

You have to use a CCSpeed action:

CCSpeed* speed= [CCSpeed actionWithAction: yourMoveAction speed: 1.0f];

// yourMoveAction is an action like CCMoveTo for example

[sprite runAction: speed];

Then you can change the speed while the sprite moves with setSpeed:

[speed setSpeed: 2.0f];

Page 1 of 1012345...10...Last »
We Can Build Your App for You. Lets Get Started !