I have a very simple app where a user can draw. A user taps 3 times and an angle will be drawn at those 3 taps. At the location of each tap, there is a circle. Below is a picture of what it looks like:
On the first tap, only a circle is created because there are no other points to connect to (as it should). On the second tap it connects a line (as it should) from the first to second tap. On the third tap, it gets weird and connects from edge of the second circle (should be in the center?) to the third tap. Any ideas why on earth this is happening??
After 3 taps:
Another example of 3 taps:
This is how i'm drawing:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self setOpacity:o];
if (CGPointEqualToPoint(first, CGPointZero)) {
first = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && CGPointEqualToPoint(second, CGPointZero)) {
second = [touch locationInView:self];
}
else if (!CGPointEqualToPoint(first, CGPointZero) && !(CGPointEqualToPoint(second, CGPointZero)) && CGPointEqualToPoint(third, CGPointZero)) {
third = [touch locationInView:self];
}
else {
first = [touch locationInView:self];
second = CGPointZero;
third = CGPointZero;
}
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGRect rectangle = CGRectMake(first.x - 10, first.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle);
CGContextMoveToPoint(context, first.x, first.y);
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGContextAddLineToPoint(context, second.x, second.y);
CGRect rectangle2 = CGRectMake(second.x - 10, second.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle2);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGContextAddLineToPoint(context, third.x, third.y);
CGRect rectangle3 = CGRectMake(third.x - 10, third.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle3);
}
CGContextStrokePath(context);
}
}
Because your current position is changed after you draw second ellipse CGContextAddEllipseInRect(context, rectangle2);
Try this:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * w = [UIColor whiteColor] ;
UIColor * r = [UIColor redColor] ;
CGContextSetStrokeColorWithColor(context, w.CGColor) ;
CGContextSetFillColorWithColor(context, r.CGColor) ;
if (!CGPointEqualToPoint(first, CGPointZero)) {
CGRect rectangle = CGRectMake(first.x - 10, first.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle);
CGContextMoveToPoint(context, first.x, first.y);
if (!CGPointEqualToPoint(second, CGPointZero)) {
CGContextAddLineToPoint(context, second.x, second.y);
CGRect rectangle2 = CGRectMake(second.x - 10, second.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle2);
CGContextMoveToPoint(context, second.x, second.y);
}
if (!CGPointEqualToPoint(third, CGPointZero)) {
CGContextAddLineToPoint(context, third.x, third.y);
CGRect rectangle3 = CGRectMake(third.x - 10, third.y - 10, 20, 20);
CGContextAddEllipseInRect(context, rectangle3);
}
CGContextStrokePath(context);
}
}
Related
I have set image's content mode aspect fit.
Now issue is that, when I'm drawing line on image, that time image's content mode set scaletofill and my image stretched. So I want solution for when I'm drawing line on image, image content mode remain same.
U can download myproject from this link.
I'm using following code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.tempImage.image==nil)
{
return;
}
colorPicker.hidden = YES;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
_pointsArray = [NSMutableArray array];
[_pointsArray addObject:NSStringFromCGPoint(lastPoint)];
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brush);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, lastPoint.x, lastPoint.y);
CGContextStrokePath(context);
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self setupExternalScreen];
UIGraphicsEndImageContext();
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.tempImage.image==nil)
{
return;
}
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
//Now set our brush size and opacity and brush stroke color:
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brush);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
CGContextSetBlendMode(context,kCGBlendModeNormal);
CGContextStrokePath(context);
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[self setupExternalScreen];
[self.pointsArray addObject:NSStringFromCGPoint(lastPoint)];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.tempImage.image==nil)
{
return;
}
//handle single tap, make _pointsArray has two identical points, draw a line between them
if (_pointsArray.count == 1) {
[_pointsArray addObject:NSStringFromCGPoint(lastPoint)];
}
[self.stack addObject:_pointsArray];
NSLog(#"color -> %# \nwidth->%f", self.selectedColor.description, brush);
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:self.selectedColor forKey:#"color"];
[dic setObject:[NSNumber numberWithFloat:brush] forKey:#"width"];
[self.contextArray addObject:dic];
[self setupExternalScreen];
[self.undoManager registerUndoWithTarget: self
selector: #selector(popDrawing)
object: nil];
}
In this code, view frame set in UIGraphicsBeginImageContext. I think it will possible using subclass of UIImageView. I don't know exact solution.
Please see this Screenshot, u will easily understand my problem
Thanks in advance
In my xcode project, the user draws an object manually. So for example they can draw a apple or triangle.
I need to save I guess the coordinates or the line path of what they draw.
How can I store the line path or re-create the image that the user has drawn. I don't want to save the image, but I want to learn from the image the user drew by knowing the specific line path?
Here is the code
.h
int mouseMoved;
BOOL mouseSwiped;
CGPoint lastPoint;
UIImageView *drawImage;
viewdidload {
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
self.view.backgroundColor = [UIColor lightGrayColor];
mouseMoved = 0;
}
.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
BOOL mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
_startPoint = [touch locationInView:self.view];
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
-(void)digitalSignatureCalled{
//viewSign.backgroundColor = [UIColor lightGrayColor];
//create a frame for our signature capture based on whats remaining
imageFrame = CGRectMake(0, 50, 500, 450);
//allocate an image view and add to the main view
mySignatureImage = [[UIImageView alloc] initWithImage:nil];
mySignatureImage.frame = imageFrame;
mySignatureImage.backgroundColor = [UIColor whiteColor];
[viewSign addSubview:mySignatureImage];
}
//when one or more fingers touch down in a view or window
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//did our finger moved yet?
fingerMoved = NO;
UITouch *touch = [touches anyObject];
//just clear the image if the user tapped twice on the screen
if ([touch tapCount] == 2) {
mySignatureImage.image = nil;
return;
}
//we need 3 points of contact to make our signature smooth using quadratic bezier curve
currentPoint = [touch locationInView:mySignatureImage];
lastContactPoint1 = [touch previousLocationInView:mySignatureImage];
lastContactPoint2 = [touch previousLocationInView:mySignatureImage];
}
//when one or more fingers associated with an event move within a view or window
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//well its obvious that our finger moved on the screen
fingerMoved = YES;
UITouch *touch = [touches anyObject];
//save previous contact locations
lastContactPoint2 = lastContactPoint1;
lastContactPoint1 = [touch previousLocationInView:mySignatureImage];
//save current location
currentPoint = [touch locationInView:mySignatureImage];
//find mid points to be used for quadratic bezier curve
CGPoint midPoint1 = [self midPoint:lastContactPoint1 withPoint:lastContactPoint2];
CGPoint midPoint2 = [self midPoint:currentPoint withPoint:lastContactPoint1];
//create a bitmap-based graphics context and makes it the current context
UIGraphicsBeginImageContext(imageFrame.size);
//draw the entire image in the specified rectangle frame
[mySignatureImage.image drawInRect:CGRectMake(0, 0, imageFrame.size.width, imageFrame.size.height)];
//set line cap, width, stroke color and begin path
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
//begin a new new subpath at this point
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), midPoint1.x, midPoint1.y);
//create quadratic Bézier curve from the current point using a control point and an end point
CGContextAddQuadCurveToPoint(UIGraphicsGetCurrentContext(),
lastContactPoint1.x, lastContactPoint1.y, midPoint2.x, midPoint2.y);
//set the miter limit for the joins of connected lines in a graphics context
CGContextSetMiterLimit(UIGraphicsGetCurrentContext(), 2.0);
//paint a line along the current path
CGContextStrokePath(UIGraphicsGetCurrentContext());
//set the image based on the contents of the current bitmap-based graphics context
mySignatureImage.image = UIGraphicsGetImageFromCurrentImageContext();
//remove the current bitmap-based graphics context from the top of the stack
UIGraphicsEndImageContext();
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//just clear the image if the user tapped twice on the screen
if ([touch tapCount] == 2) {
mySignatureImage.image = nil;
return;
}
//if the finger never moved draw a point
if(!fingerMoved) {
UIGraphicsBeginImageContext(imageFrame.size);
[mySignatureImage.image drawInRect:CGRectMake(0, 0, imageFrame.size.width, imageFrame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0f);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
mySignatureImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
//calculate midpoint between two points
- (CGPoint) midPoint:(CGPoint )p0 withPoint: (CGPoint) p1 {
return (CGPoint) {
(p0.x + p1.x) / 2.0,
(p0.y + p1.y) / 2.0
};
}
-(void)clickToSave:(id)sender{
//convert image into .png format.
viewNewSign.image = mySignatureImage.image;
if (mySignatureImage.image == nil) {
imgString = #"NoSignature";
}else{
imgString = [self getStringFromImage:mySignatureImage.image];
}
NSLog(#"image saved = %#",imgString);
NSLog(#"image length = %lu",(unsigned long)imgString.length);
viewSign.hidden = TRUE;
scroll.scrollEnabled = YES;
[scroll bringSubviewToFront:viewSign];
}
-(NSString *)getStringFromImage:(UIImage *)image{
if(image){
NSData *dataObj = UIImagePNGRepresentation(image);
//[appDelegate showAlert:#"Data Size" message:[NSString stringWithFormat:#"Data length = %lu",(unsigned long)dataObj.length]];
return [dataObj base64EncodedStringWithOptions:0];
} else {
return #"";
}
}
-(void)clickToClear:(id)sender{
[self digitalSignatureCalled];
}
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I'm trying to clear the content of what I have drawn when i press a button. But, I cant seem to do it figure out how to do it. I have google around abit and it seems like you need to do this inside of draw rect. This is the full code that I am using:
#import "PaintView.h"
#implementation PaintView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
hue = 0.0;
[self initContext:frame.size];
}
return self;
}
- (BOOL) initContext:(CGSize)size {
int bitmapByteCount;
int bitmapBytesPerRow;
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
self.cacheBitmap = malloc( bitmapByteCount );
if (self.cacheBitmap == NULL){
return NO;
}
self.cacheContext = CGBitmapContextCreate (self.cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
return YES;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self drawToCache:touch];
}
- (void) drawToCache:(UITouch*)touch {
hue += 0.005;
if(hue > 1.0) hue = 0.0;
UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];
CGContextSetStrokeColorWithColor(self.cacheContext, [color CGColor]);
CGContextSetLineCap(self.cacheContext, kCGLineCapRound);
CGContextSetLineWidth(self.cacheContext, 6);
CGPoint lastPoint = [touch previousLocationInView:self];
CGPoint newPoint = [touch locationInView:self];
CGContextMoveToPoint(self.cacheContext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(self.cacheContext, newPoint.x, newPoint.y);
CGContextStrokePath(self.cacheContext);
CGRect dirtyPoint1 = CGRectMake(lastPoint.x-10, lastPoint.y-10, 20, 20);
CGRect dirtyPoint2 = CGRectMake(newPoint.x-10, newPoint.y-10, 20, 20);
[self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
}
-(void)clear{
// this doesn't work.
CGContextClearRect(self.context, self.bounds);
}
- (void) drawRect:(CGRect)rect {
self.context = UIGraphicsGetCurrentContext();
CGImageRef cacheImage = CGBitmapContextCreateImage(self.cacheContext);
CGContextDrawImage(self.context, self.bounds, cacheImage);
CGImageRelease(cacheImage);
CGContextRetain(self.context);
}
#end
the button should call the view's setNeedsDisplay method which forces drawrect to be called or essentially forces a repaint.
I'm starting out with a straight line, as shown below.
I need the user to be able to drag and manipulate this line with their finger in real time. So they might end up with a series or curves as below:
It is simple to do this with one curve, as in the code below.
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
controlPoint = [[touches anyObject] locationInView:self];
CGRect controlRect = CGRectMake(controlPoint.x - 50.0, controlPoint.y - 50.0, 100.0, 100.0);
[self setNeedsDisplayInRect:controlRect];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, 0 , screenHeight/2 + gapDistance);
CGContextAddQuadCurveToPoint(context, controlPoint.x + gapDistance, controlPoint.y + gapDistance, 100, screenHeight/2 + gapDistance);
CGContextStrokePath(context);
}
My question is, how do I do it with multiple curves? I will also need to be able to add smaller curves to existing curves if the user so desires.
I am showing a custom alert view which is derived from UIAlertView in my view controller, and when I rotate the device for like 2-3 times, both the view controller and the alert view gets rotated. But then the app crashes with no clear clues. I have a breakpoint on All Exceptions but it can't catch it.
This crash is not happening if I use a standard UIAlertView. I found the custom alertview's code from someone else. Is there something misimplemented here? Or how can I get more clues of what's happening?
#implementation CustomAlertView
- (void) setBackgroundColor:(UIColor *) background
withStrokeColor:(UIColor *) stroke
{
if(fillColor != nil)
{
[fillColor release];
[borderColor release];
}
fillColor = [background retain];
borderColor = [stroke retain];
}
- (id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
if(fillColor == nil)
{
}
}
return self;
}
- (void)layoutSubviews
{
for (UIView *sub in [self subviews])
{
if([sub class] == [UIImageView class] && sub.tag == 0)
{
// The alert background UIImageView tag is 0,
// if you are adding your own UIImageView's
// make sure your tags != 0 or this fix
// will remove your UIImageView's as well!
[sub removeFromSuperview];
break;
}
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetLineWidth(context, 0.0);
CGContextSetAlpha(context, 0.8);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
CGContextSetFillColorWithColor(context, [fillColor CGColor]);
// Draw background
CGFloat backOffset = 2;
CGRect backRect = CGRectMake(rect.origin.x + backOffset,
rect.origin.y + backOffset,
rect.size.width - backOffset*2,
rect.size.height - backOffset*2);
[self drawRoundedRect:backRect inContext:context withRadius:8];
CGContextDrawPath(context, kCGPathFillStroke);
// Clip Context
CGRect clipRect = CGRectMake(backRect.origin.x + backOffset-1,
backRect.origin.y + backOffset-1,
backRect.size.width - (backOffset-1)*2,
backRect.size.height - (backOffset-1)*2);
[self drawRoundedRect:clipRect inContext:context withRadius:8];
CGContextClip (context);
//Draw highlight
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35, 1.0, 1.0, 1.0, 0.06 };
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace,
components, locations, num_locations);
CGRect ovalRect = CGRectMake(-130, -115, (rect.size.width*2),
rect.size.width/2);
CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint end = CGPointMake(rect.origin.x, rect.size.height/5);
CGContextSetAlpha(context, 1.0);
CGContextAddEllipseInRect(context, ovalRect);
CGContextClip (context);
CGContextDrawLinearGradient(context, glossGradient, start, end, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
}
- (void) drawRoundedRect:(CGRect) rrect inContext:(CGContextRef) context
withRadius:(CGFloat) radius
{
CGContextBeginPath (context);
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect),
maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect),
maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
}
- (void)disableDismissForIndex:(int)index_{
canIndex = index_;
disableDismiss = TRUE;
}
- (void)dismissAlert{
[self dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:YES];
}
- (void)vibrateAlert:(float)seconds{
canVirate = TRUE;
[self moveLeft];
[self performSelector:#selector (stopVibration) withObject:nil afterDelay:seconds];
}
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (disableDismiss == TRUE && canIndex == buttonIndex){
}else {
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
}
- (void)hideAfter:(float)seconds{
[self performSelector:#selector (dismissAlert) withObject:nil afterDelay:seconds];
}
- (void)moveRight{
if (canVirate){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.05];
self.transform = CGAffineTransformMakeTranslation(-10.0, 0.0);
[UIView commitAnimations];
[self performSelector:#selector (moveLeft) withObject:nil afterDelay:0.05];
}
}
- (void)moveLeft{
if (canVirate){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.05];
self.transform = CGAffineTransformMakeTranslation(10.0, 0.0);
[UIView commitAnimations];
[self performSelector:#selector (moveRight) withObject:nil afterDelay:0.05];
}
}
- (void)stopVibration{
canVirate = FALSE;
self.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
}
#end
OK now I found it. In layoutViews method, replacing [sub removeFromSuperview]; line with ((UIImageView*)sub).image = nil; fixed the problem for me. Still I'm not sure about the exact reason though.