Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2011, 09:30 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Preston, England
Posts: 20
AdColvin is on a distinguished road
Send a message via Skype™ to AdColvin
Default Problem Subclassing CALayer

I want to create some layers that have some custom properties.

So I created a class that subclassed from CALayer and I added these new properties there.

But in some cases, I had trouble accessing the properties of CALayer through this class.

I created the layers in a view class, which worked fine:

layer = [CustomLayer layer];
layer.frame = CGRectMake (0, 0, 58, 88);
layer.position = CGPointMake (250, 160);
layer.backgroundColor = [[UIColor redColor] CGColor];

But then, when I try to manipulate the view from the view controller, I get an error. For instance:

myView.layer.position = CGPointMake (20, 160);

When I use CALayer, this code works fine. But now that I'm inheriting from it, it produces an error saying that the property has not been found for this class.

Does anyone know why I'm not being allowed to do this?

Any help appreciated,
Thanks,
Adam
AdColvin is offline   Reply With Quote
Old 09-13-2011, 09:57 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by AdColvin View Post
I want to create some layers that have some custom properties.

So I created a class that subclassed from CALayer and I added these new properties there.

But in some cases, I had trouble accessing the properties of CALayer through this class.

I created the layers in a view class, which worked fine:

layer = [CustomLayer layer];
layer.frame = CGRectMake (0, 0, 58, 88);
layer.position = CGPointMake (250, 160);
layer.backgroundColor = [[UIColor redColor] CGColor];

But then, when I try to manipulate the view from the view controller, I get an error. For instance:

myView.layer.position = CGPointMake (20, 160);

When I use CALayer, this code works fine. But now that I'm inheriting from it, it produces an error saying that the property has not been found for this class.

Does anyone know why I'm not being allowed to do this?

Any help appreciated,
Thanks,
Adam
Show us the header file for your custom CALayer subclass CustomLayer
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2011, 10:30 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Preston, England
Posts: 20
AdColvin is on a distinguished road
Send a message via Skype™ to AdColvin
Default

Quote:
Originally Posted by Duncan C View Post
Show us the header file for your custom CALayer subclass CustomLayer
Of course, here it is:

Code:
@class CALayer;

@interface CustomLayer : CALayer 
{
    CFTimeInterval time;
    CGPoint startPoint;
    CGPoint endPoint;
}

@property CFTimeInterval time;
@property CGPoint startPoint;
@property CGPoint endPoint;

@end
When a layer is animating from one point to another, I need a way of figuring out its exact position on screen at any one time. These properties were to help me do that.
Thanks,
Adam
AdColvin is offline   Reply With Quote
Old 09-13-2011, 10:36 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by AdColvin View Post
Of course, here it is:

Code:
@class CALayer;

@interface CustomLayer : CALayer 
{
    CFTimeInterval time;
    CGPoint startPoint;
    CGPoint endPoint;
}

@property CFTimeInterval time;
@property CGPoint startPoint;
@property CGPoint endPoint;

@end
When a layer is animating from one point to another, I need a way of figuring out its exact position on screen at any one time. These properties were to help me do that.
Thanks,
Adam

position is probably a "fake" property that has a setter, but not an iVar. I've run into odd compiler issues with properties before where the "dot notation" won't work, but the setter form will. Try rewriting your line like this:


[myView.layer setPosition: CGPointMake(x,y)];
Code:

Also, if you want to change one of the properties of your custom subclass of CALayer, remember you need to cast the view property to your custom type before you can refer to your custom properties:

CustomLayer* myViewCustomLayer = (CustomLayer*)myView.layer;
myViewCustomLayer.time = [NSDate timeIntervalSinceReferenceDate];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2011, 10:55 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Preston, England
Posts: 20
AdColvin is on a distinguished road
Send a message via Skype™ to AdColvin
Default

Thanks for the help, I thought that had solved it. Only now, when I want to retrieve the position of the layer, by writing this:

Code:
[myView.layer position];
I get a warning telling me that there are multiple methods named "position". It doesn't do this when try to set the position.

I haven't written any methods called position myself. Any ideas why this might be happening?

Again, thanks a lot for the help.
AdColvin is offline   Reply With Quote
Old 09-13-2011, 12:03 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by AdColvin View Post
Thanks for the help, I thought that had solved it. Only now, when I want to retrieve the position of the layer, by writing this:

Code:
[myView.layer position];
I get a warning telling me that there are multiple methods named "position". It doesn't do this when try to set the position.

I haven't written any methods called position myself. Any ideas why this might be happening?

Again, thanks a lot for the help.

No, I don't know why you're getting that warning. Odd.

BTW, you should almost always declare your properties as (nonatomic).

If you don't, the system adds code that applies multi-threading locks to your getters and setters, which slows down your code unnecessarily. The only time you want your properties to be atomic is if you're reading/writing them from multiple threads at the same time.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 400
10 members and 390 guests
7twenty7, Eclectic, eski, fiftysixty, iOS.Lover, JackReidy, teebee74, tim0504, UMAD, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0