I am drawing a line in a UIView using drawRect & BezierPath. I also have a segmented controller with contentview1 and contentview2. I want the line to be displayed in content view1 but not contentview2. The code for my line is in a uiview file while the segmented control is in a uiviewcontroller. How can I successfully hide the line between the 2 content views.
InformationView.m (UIView)
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
[path addLineToPoint:CGPointMake(100.0, 100.0)];
path.lineWidth = 3;
[[UIColor blueColor] setStroke];
[path stroke];
}
InformationViewController.m (UIViewController)
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(12, 80, 300, 30);
[segmentedControl addTarget:self action:#selector(segmentAction) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
[self.view addSubview:contentView2];
[self.view addSubview:contentView ];
-(void) segmentAction {
if (segmentedControl.selectedSegmentIndex == 0) {
[contentView setHidden:NO;
[contentView2 setHidden:YES];
}
if (segmentedControl.selectedSegmentIndex == 1) {
[contentView setHidden:YES];
[contentView2 setHidden: NO];
}
I wan't to add the line to contentview so it will be hidden when another view is selected.
Add a property to InformationView
.h
#interface InformationView : UIView
#property (assign, nonatomic ,getter=isLineHidden) BOOL lineHidden;
#end
.m
#implementation InformationView
- (void)drawRect:(CGRect)rect {
if (self.lineHidden == false) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10.0, 10.0)];
[path addLineToPoint:CGPointMake(100.0, 100.0)];
path.lineWidth = 3;
[[UIColor blueColor] setStroke];
[path stroke];
}
}
-(void)setLineHidden:(BOOL)lineHidden{
if (_lineHidden != lineHidden) {
_lineHidden = lineHidden;
[self setNeedsDisplay];
}
}
#end
Then,when you want to hide line
self.yourView.lineHidden = true;
Related
I am creating one custom button class inherit from the UIControl.
I am creating Button with Kite Shape as shown in image using Bizer Path.
Code
.h
import
#interface CustomButton : UIControl
-(instancetype)initWithFrame:(CGRect)frame;
#end
.m
#import "CustomButton.h"
#implementation CustomButton
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
UIBezierPath *path = [UIBezierPath new];
// [path moveToPoint:(CGPoint){0, 0}];
[path moveToPoint:(CGPoint){100,50}];
[path addLineToPoint:(CGPoint){100, 100}];
[path addLineToPoint:(CGPoint){150, 100}];
[path addLineToPoint:(CGPoint){200, 0}];
[path addLineToPoint:(CGPoint){100, 50}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.bounds;
mask.path = path.CGPath;
// Mask the imageView's layer with this shape
self.layer.mask = mask;
}
return self;
}
#end
VC .m
CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] ;
[self.view addSubview:button];
}
- (void)buttonPressed:(id)sender
{
NSLog(#"pressing");
}
Image
My Question Is,
When I clicked outside the button(Red Portion) It gives me a click event.
I do not want to do this.
Please help me,How can i solve it?
Thank You
I am updating my answer for UIBUtton
CustomButton.h
#import <UIKit/UIKit.h>
#interface CustomButton : UIControl
-(instancetype)initWithFrame:(CGRect)frame;
#end
CustomButton.m
#import "CustomButton.h"
#implementation CustomButton
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){100,50}];
[path addLineToPoint:(CGPoint){100, 100}];
[path addLineToPoint:(CGPoint){150, 100}];
[path addLineToPoint:(CGPoint){200, 0}];
[path addLineToPoint:(CGPoint){100, 50}];
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.bounds;
mask.path = path.CGPath;
// Mask the imageView's layer with this shape
self.layer.mask = mask;
}
return self;
}
In your controller viewDidLoad
CustomButton *buttonCustom = [[CustomButton alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[buttonCustom setBackgroundColor:[UIColor redColor]];
[buttonCustom addTarget:self action:#selector(buttonCustom:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[buttonCustom setTag:1000];
[self.view addSubview:buttonCustom];
Add these methods in your Controller
-(void)buttonCustom:(UIButton *)sender forEvent:(UIEvent*)event{
UITouch *touch = [[event touchesForView:sender] anyObject];
CGPoint location = [touch locationInView:self.view];
UIColor *color = [self colorOfPoint:location];
if([color isEqual:[UIColor redColor]]){
NSLog(#"red ");
}else{
NSLog(#"White Color");
}
}
- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.view.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
return color;
}
If you have multiple UIButton then use the tag property to distinguish your button.
im learning CoreGraphic and want to make a simple game, but stuck on drawing thing with bezier path, i wanted to draw a triangle inside a subview, but it always come out wrong, i want it to fit 1/4 of the square view
My code:
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width/2, self.mainView.frame.size.height/2)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width, 0)];
[trianglePath closePath];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.mainView.frame.size.width, self.mainView.frame.size.height)];
firstView.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
firstView.layer.mask = triangleMaskLayer;
[self.mainView addSubview:firstView];
It look like this:
If the dimensions are incorrect, you probably are creating the triangle before AutoLayout has done its job.
To ensure your self.mainView has the correct size, create your triangle in your controller viewDidLayoutSubviews method.
Also mind that viewDidLayoutSubviews may be called multiple times.
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.mainView.subviews.count == 0) {
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width/2, self.mainView.frame.size.height/2)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width, 0)];
[trianglePath closePath];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.mainView.frame.size.width, self.mainView.frame.size.height)];
firstView.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
firstView.layer.mask = triangleMaskLayer;
[self.mainView addSubview:firstView];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have table cells that can expand to show some sub-detail, and I want a downward triangle to appear to let the user knows this is possible.
Here's the code that creates the triangle view...
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath moveToPoint:CGPointMake(width, height)];
[trianglePath moveToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath moveToPoint:CGPointMake(width, 0)];
[trianglePath moveToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
and the code the implements it...
-(void)drawTriangle {
if([self.detail.subDetails count]){
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
NSLog(#"triangle origin is: %#", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
NSLog(#"triangle size is: %#", NSStringFromCGSize(self.openCloseTriangle.frame.size));
self.openCloseTriangle.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.openCloseTriangle];
[self.contentView bringSubviewToFront:self.openCloseTriangle];
}
}
The code works as expected (creates a large red rectangle) if I comment out this line:
l.path = trianglePath.CGPath;
so I suppose there's something I'm not understanding about shape layers, but I'm using basically the same drawing code in another part of my app and it works fine.
The point and rect NSLog output checks out fine, too (even though it doesn't appear):
triangle origin is: {0, 0}
triangle size is: {100, 100}
you must use addLineToPoint: instead of moveToPoint: to create the path:
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath addLineToPoint:CGPointMake(width, height)];
[trianglePath addLineToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(width, 0)];
[trianglePath addLineToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
also this line
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
is problematic: a view-property usually should be weak, as the view is owned by the view hierarchy, but if you assign the newly created view directly to such a property, it might be deallocated immediately. create a local view, add it to another view, than assign it to the property, that is weak.
my test code
#import "ViewController.h"
#interface ViewController ()
#property (weak) UIView *openCloseTriangle;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self drawTriangle];
}
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath addLineToPoint:CGPointMake(width, height)];
[trianglePath addLineToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(width, 0)];
[trianglePath addLineToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
-(void)drawTriangle {
UIView * v= [self triangularViewWithWidth:100 height:100 pointsUp:NO];
self.openCloseTriangle = v;
NSLog(#"triangle origin is: %#", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
NSLog(#"triangle size is: %#", NSStringFromCGSize(self.openCloseTriangle.frame.size));
self.openCloseTriangle.backgroundColor = [UIColor redColor];
[self.view addSubview:self.openCloseTriangle];
}
#end
I have a UIView that contains some subviews - drawn using UIBezierPath. Every subview contains a scrollview having image as mask. When I try to render image of whole view (overall), instead of making image from path, it draws image of individual rect of subviews. Let me make it clear by figure:
Original View:
What I am getting after rendering Image:
I don't know what am I missing. Please guide me. Thanks.
Code:
IrregularSquare
#interface IrregularSquare : UIView
{
UIScrollView *imageResizer;
CAShapeLayer *maskLayer;
}
#end
#implementation IrregularSquare
- (id)initWithFrame:(CGRect)frame index:(NSInteger) index
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
self.layer.backgroundColor = [UIColor clearColor].CGColor;
}
return self;
}
-(void) drawRect:(CGRect)rect
{
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 20, CGRectGetMidY(rect) - 10)];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 5, CGRectGetMaxY(rect) - 10)];
[aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) + 10, CGRectGetMaxY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) - 20, CGRectGetMaxY(rect) - 20)];
[aPath addLineToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect) - 20)];
[aPath closePath];
aPath.lineWidth = 2;
[[UIColor whiteColor] setStroke];
[aPath stroke];
[self initializeImageWithPath]; //this method adds scrollview to main view and assigns an image to scrollview with mask
}
-(void) initializeImageWithPath
{
CGRect aFrame = somevalue;
self.backImage = [[UIImageView alloc] initWithFrame:aFrame];
self.backImage.contentMode = UIViewContentModeScaleAspectFill;
self.backImage.clipsToBounds = YES;
imageResizer = [[UIScrollView alloc] initWithFrame:self.bounds];
imageResizer.showsHorizontalScrollIndicator = imageResizer.showsVerticalScrollIndicator = NO;
imageResizer.scrollEnabled = YES;
imageResizer.backgroundColor = [UIColor clearColor];
imageResizer.zoomScale = 1.0;
imageResizer.delegate = self;
imageResizer.clipsToBounds = YES;
[imageResizer addSubview:self.backImage];
[self addSubview: imageResizer];
maskLayer = [CAShapeLayer layer];
maskLayer.path = [aPath CGPath];
self.layer.mask = maskLayer;
}
IrregularShapesContainer.m
#implementation IrregularShapesContainer
-(void) drawSubviews
{
for(UIView *aView in self.subviews)
[aView removeFromSuperview];
IrregularSquare *sq1 = [[IrregularSquare alloc] initWithFrame:CGRectMake(HORIPADDING, VERTICALPADDING, (self.frame.size.width - (HORIPADDING * 2) - (SPACING * 2)) * .5, (self.frame.size.height - (VERTICALPADDING) - SPACING) * .5) index:1];
[self addSubview:sq1];
[sq1.layer display];
[self randomlyPlaceImage:sq1];
sq1.irrViewDelegate = self;
//many similar instances are added in following code
}
#end
I hope my Question is clear.. Any help is appreciated.
Is there any way we can make only the selected area of a superview transparent? I have a UIView with black (alpha 0.5) background color. I am putting some hollow circles on the UIView and want the underneath UIView to be transparent so that I could see through it. Any idea
- (id)initWithFrame:(CGRect)iFrame andHollowFrames:(NSArray *)iHollowFrames withRadius:(NSNumber *)iRadius {
if ((self = [super initWithFrame:iFrame]) != nil) {
self.hollowFrames = [[NSSet setWithArray:iHollowFrames] allObjects];
self.hollowCircleRadius = iRadius;
[self addShadowView];
}
return self;
}
- (void)addShadowView {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) cornerRadius:0];
for (NSValue *point in self.hollowFrames) {
UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.CGPointValue.x - self.hollowCircleRadius.floatValue, point.CGPointValue.y - self.hollowCircleRadius.floatValue, 2.0 * self.hollowCircleRadius.floatValue, 2.0 * self.hollowCircleRadius.floatValue) cornerRadius:self.hollowCircleRadius.floatValue];
[path appendPath:circlePath];
}
[path setUsesEvenOddFillRule:YES];
CAShapeLayer *fillLayer = [CAShapeLayer layer];
fillLayer.path = path.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor blackColor].CGColor;
fillLayer.opacity = 0.5;
[self.layer addSublayer:fillLayer];
}
Here is a UIView subclass solution. In order for this to work you must leave the superviews backgroundColor property as nil witch is the default value. If this UIView-subclass is in the storyboard then make sure you set the background color to "Clear color" in the attributes inspector.
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIColor *blackBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
CGContextSetFillColorWithColor(ctx, blackBackgroundColor.CGColor);
CGContextFillRect(ctx, rect);
CGRect transparentPart = CGRectMake(10, 10, 120, 80);
CGContextClearRect(ctx, transparentPart);
}