2

I am making a project on touch moved I want to change the alpha or visibility (whatever possible) of the image at the point where it is touched. Lets say Unhidethat particular point of image..

PS I already know how to erase the image. I am looking for unerase

Shashank Kulshrestha
  • 1,556
  • 17
  • 31
  • Did u get solution for this issue? – Arun Jan 02 '13 at 05:50
  • Yeah I got it I was planning to resolve it either way changing alpha or unerase But you saved me.... I don't think so I get any relevant and till day wch can help so I am gonna do that in the prev way you told me – Shashank Kulshrestha Jan 02 '13 at 05:55
  • Enjoy keep posting queries – Arun Jan 02 '13 at 06:14
  • Hope this solution in apple communities will help you [https://discussions.apple.com/thread/1933811?start=0&tstart=0](https://discussions.apple.com/thread/1933811?start=0&tstart=0) – wod Dec 31 '12 at 11:08

6 Answers6

2

I think this project does exactly what you're looking for : https://github.com/SebastienThiebaud/STScratchView

You have 2 images where one is the hidden image and the other one is the one that you scratch off.

Or even this project : http://www.cocoacontrols.com/platforms/ios/controls/scratch-n-see

Marius Ciocanel
  • 151
  • 1
  • 6
1

I'm sorry for not having a good format for the code, for they are copied from my blog. It takes me a lot to edit.

1 To hide the part that you touch on.

When your finger moves, erase some parts :

- (UIImage *)erasePartsMoved {
UIImage *image = nil;
UIColor *color = [UIColor whiteColor];
CGFloat width = 5.0f;
CGSize imageContextSize = self.imageView.frame.size;

UIGraphicsBeginImageContext(imageContextSize);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, width);
CGContextSetStrokeColorWithColor(context, [color CGColor]);

CGContextMoveToPoint(context, self.touchStartPoint.x, self.touchStartPoint.y);
//Do something needed.
CGContextAddLineToPoint(context, self.touchEndPoint.x, self.touchEndPoint.y);

CGContextStrokePath(context);

image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return image;}

2 Your image is not visible at first, for example, transparent.

Get the point touched, then calculate a circle with the touched point, and finally change the alpha of the pixels in the circle:

typedef struct _singleRGBA{
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;} RGBA;

To change the pixel's alpha :

void filterOpacity(UInt8 *pixelBuf, UInt32 offset, void *context){
double val = *((double*)context);
int a = offset+3;
int alpha = pixelBuf[a];
pixelBuf[a] = SAFECOLOR(alpha * val);}

I hope the above would do a favor. Funny question, I would like to do a runnable demo later.

Jason Lee
  • 3,200
  • 1
  • 34
  • 71
0

I believe alpha is the property of the entire view so all you can do is draw on that particular point... refer this may be this is what you are trying to achieve.

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
0
 - (void)viewDidLoad
 {
    //load super view
    [super viewDidLoad];

    //to decrease alpha. 
    UISwipeGestureRecognizer *swiperight=[[UISwipeGestureRecognizer  alloc]     
     initWithTarget:self action: @selector(setalpha_decrease:)];
    [swiperight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:swiperight];

    //to increase alpha.     
    UISwipeGestureRecognizer *swipeleft=[[UISwipeGestureRecognizer  alloc] 
    initWithTarget:self action: @selector(setalpha_increase:)];
    [swipeleft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:swipeleft];



  }


  -(void) setalpha_decrease:(UIPanGestureRecognizer *) gestureRecognizer {

    imgview.alpha= imgtomove.alpha-(imgtomove.alpha/10);
  }



  -(void) setalpha_increase:(UIPanGestureRecognizer *) gestureRecognizer {

    imgview.alpha= imgtomove.alpha+(imgtomove.alpha/10);

  }
nizama bunti
  • 367
  • 1
  • 4
  • 18
hardik hadwani
  • 556
  • 6
  • 24
0

Why don't you put an empty view on top which is filled with a colour or texture and then using @AnkitSrivastava answer let the touch rub away parts of the view's background, revealing the hidden image behind.

Jonathan King
  • 1,528
  • 14
  • 25
-1
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [[touches allObjects] objectAtIndex:0];
    float alpha = [t locationInView:self.view].x / self.view.frame.size.width;
    _img.alpha = alpha;
}
mas'an
  • 170
  • 6