Jerky scroll with UIButton.layer.shadowColor - iphone

I got a UITableView header with lots of buttons.
I use this to create a shadow on the buttons:
Buttona.layer.shadowColor = [UIColor blackColor].CGColor;
Buttona.layer.shadowOpacity = 0.7f;
Buttona.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
Buttona.layer.shadowRadius = 7.0f;
Buttona.layer.masksToBounds = NO;
Buttonb.layer.shadowColor = [UIColor blackColor].CGColor;
Buttonb.layer.shadowOpacity = 0.7f;
Buttonb.layer.shadowOffset = CGSizeMake(2.0f, 3.0f);
Buttonb.layer.shadowRadius = 2.0f;
Buttonb.layer.masksToBounds = NO;
... and so on for Buttonc to Buttonf.
The scroll on the table becomes jerky with all theses buttons.
Should I get rid of the effect and just Photoshop the buttons with the shadow, or am I doing something wrong here?
Thanks in advance.

I had the same problem and the solution is
[view.layer setShadowPath:[[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)] CGPath]];

Related

How to remove the saw tooth of UIView when using Quartz?

I want to add a head button to a view with some white space around it. My code is just like this:
// head button
UIButton * btnHead = [UIButton buttonWithType:UIButtonTypeCustom];
btnHead.frame = CGRectMake(0, 0, self.frame.size.width - property.userhead_cell_space / 2, self.frame.size.width - property.userhead_cell_space / 2);
btnHead.clipsToBounds = YES;
btnHead.layer.cornerRadius = btnHead.bounds.size.width / 2;
btnHead.layer.borderColor = [UIColor whiteColor].CGColor;
btnHead.layer.borderWidth = (isPad?4.0f:2.0f);
btnHead.layer.contentsScale = [[UIScreen mainScreen] scale];
btnHead.layer.backgroundColor = [UIColor whiteColor].CGColor;
[btnHead addTarget:self action:#selector(clickHead:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnHead];
But alway it has some saw tooth around it . It may only one pixel. But it looks very terrible . Just like this:
Does anybody has some tip to remove the black saw tooth ?
Way late to the party but from past experience this is a bug with corner radius and borders. I've tried using it to create circular borders around views and have noticed similar bleeding.
One workaround is to create a view for the border and then a slightly inset view for the image.
UIView* border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
border.backgroundColor = UIColor.whiteColor; // border color
border.layer.cornerRadius = border.bounds.size.width / 2;
border.layer.masksToBounds = YES;
// inset by the desired border width
UIImageView* image = [[UIImageView alloc] initWithFrame:CGRectInset(border.bounds, 2, 2)];
image.image = someImage;
image.layer.cornerRadius = image.bounds.size.width / 2;
image.layer.masksToBounds = YES;
[border addSubview:image];
[self addSubview:border];
If you want to avoid multiple views, you could add an additional background layer to the image view's layer for the border. That would need to have it's bounds negatively inset so that it drew around the image.

TTStyledTextLabel text alignment not works when transform rotation is used

I am having a problem with Three20's TTSTyledTextLabel. i have specified the view like this
//iphone properties
htmlSubtitle_ = [[TTStyledTextLabel alloc] init];
htmlSubtitle_.font = [UIFont systemFontOfSize:14];
htmlSubtitle_.contentInset = UIEdgeInsetsMake(2, 2, 2, 2);
htmlSubtitle_.backgroundColor = [UIColor clearColor];
htmlSubtitle_.textColor = [UIColor whiteColor];
htmlSubtitle_.textAlignment = UITextAlignmentCenter;
htmlSubtitle_.userInteractionEnabled = NO;
htmlSubtitle_.text = [TTStyledText textFromXHTML:#"<p><p>I dont know where this\n is going<br/> but it is ok :D</p></p>" lineBreaks:YES URLs:YES];;
htmlSubtitle_.frame = CGRectMake(0, 390, 200, 90);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
[self.view addSubview:htmlSubtitle_];
it looks like this :
And when i rotate the screen i have written this code :
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
htmlSubtitle_.frame = CGRectMake(0, 0, 90, 200);
htmlSubtitle_.transform = CGAffineTransformMakeRotation(M_PI / 2);
htmlSubtitle_.backgroundColor = [UIColor greenColor];
}
and it looks like this :
see the text alignment, the alignment has been changed to left instead of remaining in center. is somebody else having this problem? or am i doing something wrong?
I have used UIWebView to show the HTML Styled Text instead of using this TTStyledLabel crap ... Thanx everybody.

shadowOffset + CornerRadius TableView

I'm trying to put shadow and corner radius to a tableView. The cornerRadius works fine but the shadowOffset doesn't work:
CGRect shadowFrame = self.mainTableView.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
self.mainTableView.layer.shadowPath = shadowPath;
self.mainTableView.layer.shadowColor = [UIColor blackColor].CGColor;
self.mainTableView.layer.shadowOffset = CGSizeMake(0, 1);
self.mainTableView.layer.shadowOpacity = 4.0;
self.mainTableView.layer.shadowRadius = 7.0;
Any idea?
Have you tried that ?
self.mainTableView.layer.masksToBounds = NO;
Basically for this to be achieved you need to use two views.
See this link: Why masksToBounds = YES prevents CALayer shadow?

iphone - How can I produce an imageview with both rounded corner and shadow?

something exactly like above?
I know how to produce a rounded corner:
imageView.layer.cornerRadius = 10;
imageView.layer.shouldRasterize = YES;
imageView.layer.masksToBounds = YES;
For the shadow, I have tried
imageView.layer.shadowOffset = CGSizeMake(1, 1);
imageView.layer.shadowRadius = 5;
imageView.layer.shadowOpacity = 0.4;
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shouldRasterize = YES;
But imageView.layer.masksToBounds = YES; from rounded corner kills the shadow.
Another question is that how to produce a shadow exactly like shown in the image? I produced this image in photoshop, I used 120 degree as the direction of the light. But if I used the code above, and turn off maskToBounds, I can see the shadow and it is ugly.
Or can I produce a rounded corner+shadow image frame in photoshop and apply the frame to every image in my app? I think that will give better performance. shadowing and cornering the images on the fly will have terrible performance if all images are on a scroll.
Thanks
Try this :
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectMake(30, 30, 128, 192);
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 2.0;
sublayer.cornerRadius = 10.0;
[self.view.layer addSublayer:sublayer];
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = sublayer.bounds;
imageLayer.cornerRadius = 10.0;
imageLayer.contents = (id) [UIImage imageNamed:#"BattleMapSplashScreen.jpg"].CGImage;
imageLayer.masksToBounds = YES;
[sublayer addSublayer:imageLayer];
And take look at the original source
I would use two image views. A background png that has the shadow (can be re-used for every image) and the foreground png image which has rounded corners.
You probably want to use a different layer for shadowing, and keep your masksToBounds and rounding code. In this example imageView is the name the image view that you want to shadow and round:
CALayer *shadowLayer = [[[CALayer alloc] init] autorelease];
sublayer.frame = imageView.bounds;
sublayer.masksToBounds = NO;
sublayer.shadowOffset = CGSizeMake(1, 1);
sublayer.shadowRadius = 5;
sublayer.shadowOpacity = 0.4;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shouldRasterize = YES;
[imageView.layer insertSublayer:shadowLayer atIndex:0]; // Put it underneath the image
That should give you the shadow. Now to avoid the slow recalculating, I suggest creating a UIImage out of the image. See this link: Create UIImage from shadowed view while retaining alpha?.
Hope this helps!

Adding outline shadow to a UIView which is iOS 3.0 compatible

I have two UIViewControllers A and B and they are added as subviews in the AppDelegate with B on top of A.
When a UIButton on B is tapped B slides off to the left with the following code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(slideOutFinished)];
simonSaysView.view.frame = CGRectMake(40-BView.view.frame.size.width, BView.view.frame.origin.y, BView.view.frame.size.width, BView.view.frame.size.height);
[UIView commitAnimations];
On the right edge I want a 20px drop shadow but i cannot use the shadow properties of BView.view.layer.shadow.... because its 3.2/4.0+ only. And the performance of the animation is awful, the slide is lagging extremely much (Its fluent without the shadow).
I'm thinking using a custom UIView and do some magic in drawRect but is this possible or can i only draw within the bounds of the view?
Hope some of you guys and girls can help me.
Cheers
objneodude
Solved with the following code
// Add drop shadow to the view.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(self.view.frame.size.width, 0, 30, self.view.frame.size.height);
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor blackColor].CGColor,
(id)[UIColor clearColor].CGColor,
nil];
gradientLayer.startPoint = CGPointMake(-2, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
[self.view.layer addSublayer:gradientLayer];
I had the same problem, after trying various ideas, I just added a subview a shadow colour (Gray) behind the UIView I needed to have a shadow, see code: (Included is the code for >3.2.x/4.x as well.
/*Shaded/rounded borders on subviews*/
self.viewOne.layer.cornerRadius = 10;
self.viewOne.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewOne.layer.borderWidth = 0.8;
self.viewTwo.layer.cornerRadius = 10;
self.viewTwo.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewTwo.layer.borderWidth = 0.8;
self.viewThree.layer.cornerRadius = 10;
self.viewThree.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.viewThree.layer.borderWidth = 0.8;
if (SYSTEM_VERSION_LESS_THAN(#"3.2")) {
self.shadowOne.layer.cornerRadius = 10;
self.shadowTwo.layer.cornerRadius = 10;
self.shadowThree.layer.cornerRadius = 10;
}
else{
//Use QuartzCore to make shadows etc.
self.viewOne.layer.shadowColor = [[UIColor grayColor] CGColor];
self.viewOne.layer.shadowOffset = CGSizeMake(2, 2);
self.viewOne.layer.shadowOpacity = 0.6;
self.viewOne.layer.shouldRasterize = YES;
self.viewOne.layer.shadowRadius = 2;
self.viewTwo.layer.shadowColor = [[UIColor grayColor] CGColor];
self.viewTwo.layer.shadowOffset = CGSizeMake(2, 2);
self.viewTwo.layer.shadowOpacity = 0.6;
self.viewTwo.layer.shouldRasterize = YES;
self.viewTwo.layer.shadowRadius = 2;
self.viewThree.layer.shadowColor = [[UIColor grayColor] CGColor];
self.viewThree.layer.shadowOffset = CGSizeMake(2, 2);
self.viewThree.layer.shadowOpacity = 0.6;
self.viewThree.layer.shouldRasterize = YES;
self.viewThree.layer.shadowRadius = 2;
}