我正在制作触摸移动的项目我希望在触摸它的位置更改图像的alpha或可见性(无论可能)。让我们说Unhidethat特定的形象点..
PS我已经知道如何擦除图像了。我正在寻找unerase
答案 0 :(得分:2)
我认为这个项目完全符合您的要求:https://github.com/SebastienThiebaud/STScratchView
你有两张图片,其中一张是隐藏的图片,另一张是你要刮掉的图片。
甚至这个项目:http://www.cocoacontrols.com/platforms/ios/controls/scratch-n-see
答案 1 :(得分:1)
我很抱歉没有合适的代码格式,因为它们是从我的博客中复制的。 编辑需要我很多。
当你的手指移动时,擦除一些部分:
- (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;}
触摸点,然后用触摸点计算圆圈,最后更改圆圈中像素的alpha值:
typedef struct _singleRGBA{
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;} RGBA;
要更改像素的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);}
我希望上面的内容会有所帮助。 有趣的问题,我想稍后做一个可运行的演示。
答案 2 :(得分:0)
我相信alpha是整个视图的属性,所以你所能做的就是利用那个特定的点...参考这可能是this你想要实现的目标。
答案 3 :(得分: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);
}
答案 4 :(得分:0)
为什么不在顶部放置一个用颜色或纹理填充的空视图,然后使用@AnkitSrivastava回答让触摸擦掉视图背景的一部分,露出背后隐藏的图像。
答案 5 :(得分:-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;
}