Trying to pass CGrect to performSelector withObject - iphone

I'm trying to pass a CGRect:
SEL frameSel = NSSelectorFromString(#"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];
But this does not compile
I also tried:
SEL frameSel = NSSelectorFromString(#"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];
Actually, this does compile but when I debug, the frame is not setted correctly:
po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>
But it should be frame = (10 10; 200 100)
How can I solve this problem?
Thank you in advance!

CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];
But this does not compile
Correct, because rect is a structure, not a pointer to an object.
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];
Actually, this does compile but when I debug, the frame is not setted correctly:
po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>
Well, it's not garbage, so it would seem that it worked, even though the origin somehow remained zero. You may want to ask a separate question about that.
Anyway, why are you using performSelector:withObject:? You're not addressing it to a specific thread and you're not putting it on a delay; why not simply say object.frame = rect?

You must use an NSInvocation to handle any non-id arguments or return values:
CGRect rect = CGRectMake(104, 300, 105, 106);
UIView *view = [[UIView alloc] init];
NSMethodSignature *methodSig = [view methodSignatureForSelector:#selector(setFrame:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
invocation.target = view;
invocation.selector = #selector(setFrame:);
[invocation setArgument:&rect atIndex:2];
[invocation invoke];

Related

To make 16 uilabels and align them in circular path.

NSMutableArray *views = [[NSMutableArray alloc]initWithCapacity:0];
for (NSInteger i = 0; i<16; i++)
{
UIView *circle = [[UIView alloc]init];
circle.backgroundColor = [UIColor clearColor];
UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
circleImage.image = [UIImage imageNamed:#"circle"];
[circle addSubview:circleImage];
UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
labelInsideCircle.backgroundColor = [UIColor clearColor];
labelInsideCircle.textColor = [UIColor greenColor];
labelInsideCircle.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelInsideCircle.center = circleImage.center;
NSInteger int_ = [self getRandomNumber:0 to:(arrOfOptions.count-1)];
labelInsideCircle.text = [NSString stringWithFormat:#"%#",[arrOfOptions objectAtIndex:int_]];
labelInsideCircle.textAlignment = NSTextAlignmentCenter;
[arrOfOptions removeObjectAtIndex:int_];
[circle addSubview:labelInsideCircle];
[labelInsideCircle release];
[views addObject:circle];
[circle release];
[circleImage release];
}
/* Rotating circles with angles */
float curAngle = 0;
float incAngle = ( 360.0/(views.count) )*3.14/180.0;
CGPoint circleCenter = CGPointMake(380, 580); /* given center */
float circleRadius = 250; /* given radius */
for (UIView *view in views)
{
CGPoint viewCenter;
viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
view.transform = CGAffineTransformRotate(view.transform, curAngle);
view.center = viewCenter;
[self.view addSubview:view];
curAngle += incAngle;
}
The problem is here the text of UILabel is also getting transformed, which is obvious. What I want is 16 circular views with labels on them without the label's text transformed. Can anyone please help me out with this ?
In this case, you just need to change their location coordinates, not rotate them.
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:0];
for (NSInteger i = 0; i<16; i++)
{
UIView *circle = [[UIView alloc]init];
circle.backgroundColor = [UIColor clearColor];
UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
circleImage.image = [UIImage imageNamed:#"circle"];
[circle addSubview:circleImage];
UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
labelInsideCircle.backgroundColor = [UIColor clearColor];
labelInsideCircle.textColor = [UIColor greenColor];
labelInsideCircle.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelInsideCircle.center = circleImage.center;
NSInteger int_ = arc4random()%[arrOfOptions count];
labelInsideCircle.text = [NSString stringWithFormat:#"%#",[arrOfOptions objectAtIndex:int_]];
labelInsideCircle.textAlignment = NSTextAlignmentCenter;
[arrOfOptions removeObjectAtIndex:int_];
[circle addSubview:labelInsideCircle];
[labelInsideCircle release];
[views addObject:circle];
[self.view addSubview:circle];
[circle release];
[circleImage release];
}
/* Rotating circles with angles */
float curAngle = 0;
float incAngle = ( 360.0/(views.count) )*3.14/180.0;
CGPoint circleCenter = CGPointMake(380, 580); /* given center */
float circleRadius = 250; /* given radius */
for (UIView *view in views)
{
CGPoint viewCenter;
viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
//view.transform = CGAffineTransformRotate(view.transform, curAngle);
view.center = viewCenter;
[self.view addSubview:view];
curAngle += incAngle;
}
Some coding suggestions:
You can use arc4random() function if you don't have anything special in your own random number generator.
You can turn on ARC in xCode!
In a loop set their centers accordingly, don't rotate them nor their super view.
The following is not directly related to your question but may be helpful:
Aparaently you have got all the math available. I suggest to measure how much cpu time gets lost on the cos and sin etc. calls. If you find that significant then think about your algorithm. You will (most probably) find out that you call cos and sin hundrets or thousands of times for a limited number of angles.
You may then try it and find out that possible pre-calculations or just "caching and reusing" earlier results may save significant processing time.
Plus pre-calculating or caching of sinus would do. You can derrive cosinus values from sinus (and vice versa) by adding (or substracting respectively) an offset of pi/2 (or 90 degrees respectively) to the argument.
For similar tasks I was working with degrees (not radiants) and found out that I could not predict the angles but that a significant of full degrees (1°, 2°, 3°, ... and nothing in between) was exact enough for the job. Then I maintained an array of 360 sinus values and used that instead of calling the real function again and again. (Plus I only had to calculate 90 of them and mirrored the results of the other 270 degrees according to the nature of the sinus function) 180 floats is not too much of memory compared to the speed that I gained. Something like that can be suitable for you too. In your case your potenital agruments to sin and cos are limited to full-number multipilers of incAngle.
Which means there are only [views count] number of potential values each.

Find real frame of UIVIew

I have a custom UIView I create in my class. I then add it to my screen and I want to know it's CGRect. But from what it seems, I can't seem to get the size of this particular UIView. Here's my code:
CGRect labelFrame;
if (!tickerImage) {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, 0, 0);
} else {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, 0, 0);
}
SGNewLabel = [[SGAdressLabel alloc] initWithFrame:labelFrame];
[labelsArray addObject:SGNewLabel];
[self.view addSubview:SGNewLabel];
[SGNewLabel addNewLabelWithText:labelText andTickerImage:tickerImage];
UIView *lastLabel = [labelsArray lastObject];
CGRect newTextFieldFrame = CGRectMake(lastLabel.frame.origin.x + lastLabel.frame.size.width, labelFrame.origin.y, 320, 30);
upperTextField.frame = newTextFieldFrame;
As you see, I set the upperTextField frame based on the frame of my custom SGNewLabel which appears to be wrong, even if put into NSArray. I set 0, 0 at the end of the labelFrame declaration because I don't know the size of the future object and here is the source of my problem.
How can I correct it and get the right size of my UIView?
EDIT:
NSLog on my view gives me this:
NSLog(#"The rect of object is: %#", NSStringFromCGRect(self.frame));
2013-02-11 17:16:02.333 MyApp[2383:c07] The rect of object is: {{24, 55}, {0, 0}}
The UIView itself is drawn normally and I can see it full-sized.
I should have used my label's frame set in its own class and then my code would look like this:
SGNewLabel = [[SGAdressLabel alloc] init];
CGRect labelFrame;
if (!tickerImage) {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 8, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
} else {
labelFrame = CGRectMake(upperTextField.frame.origin.x + 16, upperTextField.frame.origin.y, SGNewLabel.frame.size.width, SGNewLabel.frame.size.height);
}
SGNewLabel.frame = labelFrame;

sizeWithFont:constrainedToSize:lineBreakMode: not accurate?

sizeWithFont:constrainedToSize:lineBreakMode: don't seem to be returning me the correct width. After these codes are executed, I see that part of the string in the label is chopping off, which means I've to manually add a few pixels to the size. Am I missing something?
I've a UILabel:
theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, LABELWIDTH, LABELHEIGHT)];
theLabel.lineBreakMode = UILineBreakModeWordWrap;
theLabel.numberOfLines = 0;
[theLabel setFont:[UIFont fontWithName:#"MarkerFelt-Wide" size:16]];
theLabel.textColor = [UIColor blackColor];
theLabel.textAlignment = UITextAlignmentLeft;
theLabel.backgroundColor = [UIColor clearColor];
I tried to programmatically modify the size of the label using the following:
CGSize maximumLabelSize = CGSizeMake(LABELWIDTH, 10000);
CGSize expectedLabelSize = [someString sizeWithFont:theLabel.font constrainedToSize:maximumLabelSize lineBreakMode:theLabel.lineBreakMode];
theLabel.text = someString;
CGRect newFrame = theLabel.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = newFrame.size.width+50;
theLabel.frame = newFrame;
Ok, well, the first thing I'll say is that there are some very useful ways to deal with frames that you currently aren't employing. For example, your code,
CGRect newFrame = theLabel.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = newFrame.size.width+50;
theLabel.frame = newFrame;
Can be rewritten with functions from CGGeometry,
CGFloat widthOffset = 50.0f;
theLabel.frame = CGRectOffset(CGRectInset(theLabel.frame, widthOffset, 0.0f), widthOffset / 2.0f, 0.0f);
However, if your code worked as it was intended, you would not need to do this at all. You can go two routes,
[theLabel sizeToFit];
Or, this should also work,
theLabel.frame = CGRectMake(theLabel.frame.origin.x, theLabel.frame.origin.y, expectedLabelSize.width, expectedLabelSize.height);
No where in your earlier code did you change the width of theLabel to match the expected width. Notice, you wrote newFrame.size.width = newFrame.size.width+50 and that should be newFrame.size.width = expectedLabelSize.width.

How to make something like iPhone Folders?

I'm wanting to know if there's a way I can transform my view to look something like iPhone folders. In other words, I want my view to split somewhere in the middle and reveal a view underneath it. Is this possible?
EDIT:
Per the suggestion below, I could take a screenshot of my application by doing this:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Not sure what to do with this, however.
EDIT:2
I've figured out how to add some shadows to my view, and here's what I've achieved (cropped to show relevant part):
EDIT:3
http://github.com/jwilling/JWFolders
the basic thought will be to take a picture of your current state and split it somewhere. Then animate both parts by setting a new frame. I don't know how to take a screenshot programmatically so I can't provide sample code…
EDIT: hey hey it's not looking great but it works ^^
// wouldn't be sharp on retina displays, instead use "withOptions" and set scale to 0.0
// UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *f = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect fstRect = CGRectMake(0, 0, 320, 200);
CGRect sndRect = CGRectMake(0, 200, 320, 260); // was 0,200,320,280
CGImageRef fImageRef = CGImageCreateWithImageInRect([f CGImage], fstRect);
UIImage *fCroppedImage = [UIImage imageWithCGImage:fImageRef];
CGImageRelease(fImageRef);
CGImageRef sImageRef = CGImageCreateWithImageInRect([f CGImage], sndRect);
UIImage *sCroppedImage = [UIImage imageWithCGImage:sImageRef];
CGImageRelease(sImageRef);
UIImageView *first = [[UIImageView alloc]initWithFrame:fstRect];
first.image = fCroppedImage;
//first.contentMode = UIViewContentModeTop;
UIImageView *second = [[UIImageView alloc]initWithFrame:sndRect];
second.image = sCroppedImage;
//second.contentMode = UIViewContentModeBottom;
UIView *blank = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
blank.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:blank];
[self.view addSubview:first];
[self.view addSubview:second];
[UIView animateWithDuration:2.0 animations:^{
second.center = CGPointMake(second.center.x, second.center.y+75);
}];
You can uncomment the two .contentMode lines and the quality will improve but in my case the subview has an offset of 10px or so (you can see it by setting a background color to both subviews)
//EDIT 2: ok found that bug. Had used the whole 320x480 screen, but had to cut off the status bar so it should be 320x460 and all is working great ;)
Instead of taking a snapshot of the view, you could use a separate view for each row of icons. You'll have to do a bit more work with repositioning stuff, but the rows won't be static when the folder is open (in other words, they'll keep redrawing as necessary).
I took relikd's code as a base and made it a bit more dynamic.
You can specify split position and direction when calling the function and I added a boarder to the split images.
#define splitAnimationTime 0.5
- (void)split:(SplitDirection)splitDirection
atYPostition:(int)splitYPosition
withRevealedViewHeight:(int)revealedViewHeight{
// wouldn't be sharp on retina displays, instead use "withOptions" and set scale to 0.0
// UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *f = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect fullScreenRect = [self getScreenFrameForCurrentOrientation];
CGRect upperSplitRect = CGRectMake(0, 0,fullScreenRect.size.width, splitYPosition);
CGRect lowerSplitRect = CGRectMake(0, splitYPosition, fullScreenRect.size.width, fullScreenRect.size.height-splitYPosition);
CGImageRef upperImageRef = CGImageCreateWithImageInRect([f CGImage], upperSplitRect);
UIImage *upperCroppedImage = [UIImage imageWithCGImage:upperImageRef];
CGImageRelease(upperImageRef);
CGImageRef lowerImageRef = CGImageCreateWithImageInRect([f CGImage], lowerSplitRect);
UIImage *lowerCroppedImage = [UIImage imageWithCGImage:lowerImageRef];
CGImageRelease(lowerImageRef);
UIImageView *upperImage = [[UIImageView alloc]initWithFrame:upperSplitRect];
upperImage.image = upperCroppedImage;
//first.contentMode = UIViewContentModeTop;
UIView *upperBoarder = [[UIView alloc]initWithFrame:CGRectMake(0, splitYPosition, fullScreenRect.size.width, 1)];
upperBoarder.backgroundColor = [UIColor whiteColor];
[upperImage addSubview:upperBoarder];
UIImageView *lowerImage = [[UIImageView alloc]initWithFrame:lowerSplitRect];
lowerImage.image = lowerCroppedImage;
//second.contentMode = UIViewContentModeBottom;
UIView *lowerBoarder = [[UIView alloc]initWithFrame:CGRectMake(0, 0, fullScreenRect.size.width, 1)];
lowerBoarder.backgroundColor = [UIColor whiteColor];
[lowerImage addSubview:lowerBoarder];
int reveledViewYPosition = splitYPosition;
if(splitDirection==SplitDirectionUp){
reveledViewYPosition = splitYPosition - revealedViewHeight;
}
UIView *revealedView = [[UIView alloc]initWithFrame:CGRectMake(0, reveledViewYPosition, fullScreenRect.size.width, revealedViewHeight)];
revealedView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
[self.view addSubview:revealedView];
[self.view addSubview:upperImage];
[self.view addSubview:lowerImage];
[UIView animateWithDuration:splitAnimationTime animations:^{
if(splitDirection==SplitDirectionUp){
upperImage.center = CGPointMake(upperImage.center.x, upperImage.center.y-revealedViewHeight);
} else { //assume down
lowerImage.center = CGPointMake(lowerImage.center.x, lowerImage.center.y+revealedViewHeight);
}
}];
}
This means I can call it like this:
[self split:SplitDirectionUp atYPostition:500 withRevealedViewHeight:200];
I used these conveniance functions in the updated split function:
- (CGRect)getScreenFrameForCurrentOrientation {
return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
- (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation {
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds;
BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
//implicitly in Portrait orientation.
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
CGRect temp = CGRectZero;
temp.size.width = fullScreenRect.size.height;
temp.size.height = fullScreenRect.size.width;
fullScreenRect = temp;
}
if(!statusBarHidden){
CGFloat statusBarHeight = 20;
fullScreenRect.size.height -= statusBarHeight;
}
return fullScreenRect;
}
and this enum:
typedef enum SplitDirection
{
SplitDirectionDown,
SplitDirectionUp
}SplitDirection;
Adding a return to normaal function and adding the arrow would be a great addition.

Set UIImageView Size?

i have following problem: i generate subviews UIView in an UIScrollView including UIImageViews.
NSArray *bilder = [[NSArray alloc] initWithObjects:#"lol.jpg", #"lol2.jpg", nil];
NSArray *added = [[NSMutableArray alloc] init];
UIImageView *tmpView;
for (NSString *name in bilder) {
UIImage *image = [UIImage imageNamed:name];
tmpView = [[UIImageView alloc] initWithImage:image];
tmpView.contentMode = UIViewContentModeScaleAspectFit;
tmpView.frame = CGRectMake(0, 0, 300, 300);
[added addObject:tmpView];
[tmpView release];
}
CGSize framy = mainFrame.frame.size;
NSUInteger x = 0;
for (UIView *view in added) {
[mainFrame addSubview:view];
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
}
mainFrame.scrollEnabled = YES;
mainFrame.pagingEnabled = YES;
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
mainFrame.backgroundColor = [UIColor blackColor];
i get image file names out of an array. now i want to resize the imageview to a specific size, but it won't work. any advice?
thanks + regards
UIImage lacks the setSize: method of NSImage, but this post seems to add a scaleToSize: method using a category on UIImage.
Change tmpView.frame to use a new CGRect:
tmpView.frame = CGRectMake(tmpView.frame.origin.x,
tmpView.frame.origin.y,
newWidth,
newHeight);
What I see is:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
But if you look at CGRectMake...
Declaration: CGRect CGRectMake (
CGFloat x,
CGFloat y,
CGFloat width,
CGFloat height
);
You are setting the X value, not the Y value. I expect, based on your later call to:
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
that you intend to make the these images appear on top of each other, not side by side.
Suggest:
view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
to
view.frame = CGRectMake(0,framy.height * x++, framy.height, framy.width);