Let said I have a image which the circle in the middle is transparent and I use the drawRect function to draw sqaure like the second image below.
Then what I want to do is to get the alpha layer of the first image, the circle as a path, then apply on the square i draw and clip it and draw like the final image, just want it to draw on the circle path.
Thank in advance.
Related
I have a UIImageView displaing an image. This view's layer is masked with CAShapeLayer in order to create circular "hole" in the image. To create the hole I use UIBezierPath with .usesEvenOddFillRule = true.
It works fine when static. But I need that hole to move with user finger. To do that I create new UIBezierPath with even-odd rule each time user moves their finger. On smaller phones with smaller images it looks OK but on iPhone 6 Plus it is choppy.
Any ideas on how to make it smooth are very wellcome. I cannot just move the frame of masking CAShapeLayer - it would move the hole bot also hide some edges of the image. So the only way is to change its .path each time user moves finger and that is slow.
EDIT: matt's answer would work in some scenarios but not in my case: I am not displaying the whole image only a part of it defined by UIBezierPath. This part is most often oval (but can be rectangular or rounded rectangle) and it has "hole" cut in it. While the hole is mowing with users finger the displayed part/shape of the image does not change - it is static.
The ineficient solution that was in place so far wa:
Create UIBezierPath with boundary of displayed part of the image
Set 'even off fill rule' on it
Add UIBezierPath of the hole to it
Set it as path of CAShapeLayer with some opaque fill color
Use that CAShapeLayer as mask of the UIImageView
This procedure was repeated each time a user moved their finger. I cannot simply move the whole mask layer as that would also change the part of the image being displayed. I what it to stay static and move only the hole in it.
it would move the hole bot also hide some edges of the image
Well, I don't agree. Moving the mask is exactly the way to do this. I don't see why you think there's a problem with that. Perhaps the issue is merely that you have not made the mask layer big enough. It does not have to be the same size as the layer it is masking. In this case, it needs to be about 9 times the size of the masked layer (3 horizontal and 3 vertical), so that it will continue to cover the masked the layer no matter how far in any direction the user slides it.
I am trying to create an animated radial chart that looks like the activity app created by Apple. I provide an image to show what I would like as result:
Do you know how to obtain this result? If you have any idea could you please focus on the following points?
Create the gradient inside each circle
Create the shadow on the head of the circle
Thank you very much in advance.
Check out my custom control, I tried to make it as close as possible to the Activity app design, and everything is customizable.
https://github.com/maxkonovalov/MKRingProgressView
The basic idea behind the algorithm is quite simple.
To draw arc line with changing color:
Generate a conical gradient image
You can use a prerendered image from Photoshop or generate your own dynamically. I use a gradient image generator from here: https://github.com/maxkonovalov/MKGradientView.
Clip gradient image to show only the arc
CGContextSaveGState(ctx)
CGContextAddPath(ctx, arcPath)
CGContextClip(ctx)
CGContextDrawImage(ctx, gradientRect, gradientImage)
CGContextRestoreGState(ctx)
Drawing the shadow is even simpler:
Draw shadow behind your progress arc (shown with 50% opacity here)
I draw a circle shape matching arc's end for the shadow.
CGContextSetShadow(ctx, offset, shadowRadius)
CGContextAddPath(ctx, shadowPath)
CGContextSetFillColorWithColor(ctx, shadowColor)
CGContextFillPath(ctx)
Clip the shadow to fit into the progress circle
CGContextSaveGState(ctx)
CGContextAddPath(ctx, circlePath)
CGContextClip(ctx)
// Draw shadow...
CGContextRestoreGState(ctx)
And the final result looks like this:
This isn't quite what you need as it appears to generate the image for you, but you could get some good info from the source.
https://github.com/hmaidasani/RadialChartImageGenerator
How can I draw a rectangle that has two round corners and the opposite corners are rectangular corners. The shape must be closed so it can be filled with the Brush color. The Polyline method doesn't draw curved lines. Can I add the points of an Arc to the polyline points? I tried to draw a RoundRect using Canvas method and then, overlapping a rectangle over the lower round corners, but I couldn't figure out how to erase the upper line of the rectangle when drawign just the border of the shape without filling it. Note: if you think is relevant, I can add the code I used.
Sample of the desired shape:
Sample of what I got with Delphi:
You don't have to fill the shape at the same time you draw it. You can use a series of TCanvas.LineTo() and TCanvas.ArcTo()/TCanvas.AngleArc() calls first to create the shape, then call TCanvas.FloodFill() afterwards to fill it.
Otherwise, you can overlap TCanvas.Rectangle() on top of TCanvas.RoundRect() with the same fill color, and then use TCanvas.MoveTo()/TCanvas.LineTo() to draw over the dividing line with the same fill color.
Another option would be to forget using TCanvas drawing methods and just use Win32 API calls instead. Use CreateRoundRectRgn(), CreateRectRgn(), and CombineRn() to create a HRGN that has your desired shape, then use FillRgn() and FrameRgn() to draw on your TCanvas using that HRGN.
I have an UIImageview that contains a circle (The obstacle) and then another UIImageview that contains another image (my character). When my circle hits my character the game ends.
My problem occurs when the game ends on the character touching the UIImageview box that my circle is within, rather than the circle inside the UIImageview box.
The solutions I can think of are:
Make the UIImageview rounded to fit my circle.
Somehow detect a collision between my characters pixels and the circles pixels rather than the UIImageviews.
Help and ideas would be really appreciated. I am a beginner with xcode.
Thanks!
Couple of different techniques:
1)UIView#layer.cornerRadius - Super simple to implement, just set that one property. But really bad for scrolling elements like table views.
2)UIImageView as a mask - Also easy to implement. Basically make a square image with a transparent circle in the middle and slap it on top of your view with a new UIImageView. If your circle design has some reflections or styling, this might make your life easier.
3)Custom drawing - Make a UIView subclass and override drawRect to draw your image and clip it to a circular path.
Hope that helps!
EDIT:
You can read this answers.
1)Fill an UIBezierPath with a UIImage
2)iOS: Inverse UIBezierPath (bezierPathWithOvalInRect)
3)How to mask a square image into an image with round corners in the iPhone SDK?
How to rotate triangle image of speech bubble on the surface of bubble ? triangle image can scale and rotate with the touches and dragging.
What you can do is clip the triangle part of the bubble form the original image, make it into a UIImageView and place it just as if it is a part of the bubble. Then use this UIImageView and make it scale , rotate etc likewise according to UIGestures or UITouch whichever is easier for you along with the bubble. If you want any code help for that please ask. Thanks :)