I Have Some CALayers in my TableViewCell, when I touch the layer and start to drag, I found that, I can not drag the table.
Do I have to use an UIView instead of CALayer?
Any sugguestion?
CALayer is not a subclass of UIResponder so they can't handle any touches. Use UIView instead.
Related
How to add UIButtons , UILabels UIImageView on CALayer and perform actions according to the button pressed.Thanks in advance
{
[graphic addSublayer:self.firstButton.layer];
[graphic addSublayer:self.mylabel.layer];
[self.view.layer addSublayer:graphic];
}
I used the above code but when I pressed the button it does not perform any action
Thanks
A CALayer is not an event responder, If you want a button that actually works on top of a CALayer, put that CALayer into a UIView and add a UIButton to that view.
I have a UIView in which I draw things in the drawRect method. I would like to overlay a CALayer over the UIView CALayer (self.layer) that would draw a subset of the things I draw in the drawRect. Basically, I draw a lot of circles in drawRect, and I would like to highlight some them on the overlay. I get the coordinates of the circles from a model object that is a property of the UIView.
My first attempt was to add a sublayer s to the UIView layer, set its delegate to the UIView and call its setNeedsDisplay method when needed, but drawLayer:InContext is called by both layers (s and self.layer). This doesn't work:
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
if (layer == s ) {
// draw stuffs for s
}
else if (layer==self.layer) {
// do nothing
}
}
results in a black UIView. I would like to draw s from a place where I have access to my data model, and other variables useful for the drawing (width of lines..). I have kinda solved the problem by moving the drawing of s to a separate object, but this forces me to also set a pointer of the model in this object, and also copy other parameters.
So my question is: in a UIView drawing its content using drawRect, how to add a sublayer which delegate is the same UIView ?
You need to subclass UIView and in that subclass override + (Class)layerClass with your own CALayer subclass. Then that UIView will use that subclass when it creates its backing layer. Your CALayer subclass would then have an override for drawLayer.
You can put this CALayer subclass (interface and implementation) in your UIView subclass.
I have view1 which is an UIImageView and I also have view2 which is a subview of view1 and is a UIImageView too.
I want to move view2 around and I would like it to have alpha=0 where view1 (his superview) has alpha!=1, is that possible? how can I do that?
possible to move/drag view2 through different ways.
Use UItouch delegates to do it.. touchesBegan, touchesMoved,touchesEnded,touchesCancelled etc..
You can do it 2 diffrents ways.
1.Create a child class which is inherited from UiView named as view2 and implement the above touch delegate and create its obj and add as the subview of your view1.Then you can get the touch delegate working when touch occurred in view2.
in touchesBegan,touchesMoved delegates, do the coding for drag opertaions.
2.in main view touchesBegan,touchesMoved delegates, check the if touch point intersect the view2 frame.
if yes,do the code for view2 drag
for drag and drop please see the below links
drag-uiview-between-uiviews
how-to-drag-and-drop-images
MoveMe : Apple developer
For setAlpha, you can use UIView method yourUiView.setAlpha = 1.0 or yourUiView.setAlpha = 0.0 as per your conditions
Is it possible to animate a UIView's CoreGraphics content?
Say I have a UIView subclass called MyView that implements the drawRect: method like so:
- (void) drawRect: (CGRect) rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(c, someColor);
CGContextSetLineWidth(c, someWidth);
CGContextMoveToPoint(c, leftOfMyUIView, topOfMyUIView);
CGContextAddLineToPoint(c, leftOfMyUIView, bottomOfMyUIView);
CGContextStrokePath(c);
}
In other words - it draws a vertical line down the left hand side of my subclass of UIView. Is there an easy way to now animate this UIView so that the line moves from the left side to the right side of the view? I would like it to 'drift' from left to right.
To be clear - for reasons I will not go into I cannot move/animate the instance of MyView. The best solution I can think of using is to add a new UIView subclass. Something like LineView extends UIView and then make LineView the exact dimensions of the line I want to draw, fill it using its own drawRect method and then add an instance of LineView to MyView as a subview. This would allow me to animate the position of the LineView object using an animation block, but seems overly complicated to achieve something so simple.
Based upon your comment that there will be hundreds of LineView instances, it may be best to use CALayer subclasses as opposed to UIView subclasses. UIView objects give you event handling, and on iOS each UIView owns a CALayer to handle its rendering. If you don't want/need event tracking for the individual lines, all those UIView instances are probably needless overhead. You can access the parent UIView's layer, and add your line sublayers as needed.
If I understand your problem correctly, there should be no need for CoreGraphics. You can set the line layers' backgroundColor to handle the fill. Or if the lines will not be straight, you can use CAShapeLayer to render the paths of the lines.
I've uibutton under transparent uiview, how can I determine touchUpInside event?
Set the property userInteractionEnabled of the transparent UIView to NO and it should pass all touches to your UIButtons.