他の部分が半透明で、中央が完全に透明な画像ビューは必要ありません。この画像を参照してください。
私はそれをなんとかすることができましたが、それは最善の方法ではありません..黄色い線の内側の長方形だけを透明にすることはできますか...?
編集:ビューの特定の長方形のアルファを変更することは可能ですか..?
他の部分が半透明で、中央が完全に透明な画像ビューは必要ありません。この画像を参照してください。
私はそれをなんとかすることができましたが、それは最善の方法ではありません..黄色い線の内側の長方形だけを透明にすることはできますか...?
編集:ビューの特定の長方形のアルファを変更することは可能ですか..?
カスタムUIViewをビューの上に配置して、画像、サイズを表示し、正確に重なるように配置します。カスタムビューのdrawRectメソッドで
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect rBounds = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
// Fill background with 80% white
CGContextSetFillColorWithColor(context, [[[UIColor whiteColor] colorWithAlphaComponent:0.8] CGColor]);
CGContextFillRect(context, rBounds);
// Draw the window 'frame'
CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
CGContextSetLineWidth(context, 10);
CGContextStrokeRect(context, self.maskRect);
// make the window transparent
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillRect(context, self.maskRect);
}
ここで、maskRectは、透明に表示される長方形です。カスタムビューの背景色が「clearColor」に設定されていることを確認してください
maskRectを変更し、このビューで「setNeedsDisplay」を呼び出すために必要な追加のロジックを記述できます。
たくさんの努力の末、私は解決策を見つけました。
これがそのコードです!!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[screenShotButton setHidden:YES];
UITouch *touch = [touches anyObject];
CGPoint pnt =[touch locationInView:self.view];
CGRect viewFrame=changeView.frame;
CGFloat x1Diff,y1Diff,x2Diff,y2Diff;
x1Diff=ABS(viewFrame.origin.x-pnt.x);
y1Diff=ABS(viewFrame.origin.y-pnt.y);
x2Diff=ABS(viewFrame.origin.x+viewFrame.size.width-pnt.x);
y2Diff=ABS(viewFrame.origin.y+viewFrame.size.height-pnt.y);
if (x1Diff<=10) {
left=YES;
shouldExpand=YES;
}
if (y1Diff<10) {
top=YES;
shouldExpand=YES;
}
if (x2Diff<=10) {
right=YES;
shouldExpand=YES;
}
if (y2Diff<=10) {
bottom=YES;
shouldExpand=YES;
}
if(CGRectContainsPoint(changeView.frame, pnt))
{
shouldChange=YES;
fromPoint=pnt;
}
else
{
shouldChange=NO;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!shouldChange) {
return;
}
if (shouldExpand)
{
UITouch *touch = [touches anyObject];
CGPoint pnt =[touch locationInView:self.view];
CGRect preFram =changeView.frame;
CGRect modifiedFrame=preFram;
if (left) {
modifiedFrame.origin.x=preFram.origin.x-(fromPoint.x-pnt.x);
modifiedFrame.size.width=preFram.size.width+fromPoint.x-pnt.x;
fromPoint.x=pnt.x;
}
if (right) {
modifiedFrame.size.width=preFram.size.width+pnt.x-fromPoint.x;
fromPoint.x=pnt.x;
}
if (top) {
modifiedFrame.origin.y=preFram.origin.y-(fromPoint.y-pnt.y);
modifiedFrame.size.height=preFram.size.height+fromPoint.y-pnt.y;
fromPoint.y=pnt.y;
}
if (bottom) {
modifiedFrame.size.height=preFram.size.height+pnt.y-fromPoint.y;
fromPoint.y=pnt.y;
}
changeView.frame=modifiedFrame;
[clearImage setBounds:CGRectMake(0-modifiedFrame.origin.x, 0-modifiedFrame.origin.y, clearImage.frame.size.width, clearImage.frame.size.height)];
}
else
{
UITouch *touch = [touches anyObject];
CGPoint pnt =[touch locationInView:self.view];
changeView.center=CGPointMake(changeView.center.x-fromPoint.x+pnt.x, changeView.center.y-fromPoint.y+pnt.y);
[clearImage setBounds:CGRectMake(0-changeView.frame.origin.x, 0-changeView.frame.origin.y, clearImage.frame.size.width, clearImage.frame.size.height)];
fromPoint=pnt;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[screenShotButton setHidden:NO];
left=top=right=bottom=NO;
shouldExpand=NO;
fromPoint=CGPointZero;
toPoint=CGPointZero;
shouldChange=NO;
}
- (IBAction)takeScreenShot:(id)sender
{
[screenShotButton setHidden:YES];
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1.0, 1.0);
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/1.png",docPath];
CGRect cutRect = changeView.frame;
CGImageRef imgRef = CGImageCreateWithImageInRect([img CGImage], cutRect);
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
CGImageDestinationAddImage(destination, imgRef, nil);
CFRelease(imgRef);
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"Failed to write image to %@", filePath);
}
img=nil;
imgRef=nil;
CFRelease(destination);
[screenShotButton setHidden:NO];
[changeView setHidden:NO];
}
ただ、ここではロジックが重要です!!
なぜあなたはこれだけのようにそれをしないのですか?
選択した長方形の外側を変更できるかもしれません。下の画像は、4つの長方形の4つのアルファが必要です。たとえば、最初に白いウィンドウの背景色を設定し、次に長方形の4つのアルパを0.5に設定します。
UIViewの代わりにCALayerを使用したほうがよいと思います。
私のアドバイスがあなたの少しの助けになることを願っています。
CGContextが使えると思います
void CGContextDrawRadialGradient(
CGContextRef context,
CGGradientRef gradient,
CGPoint startCenter,
CGFloat startRadius,
CGPoint endCenter,
CGFloat endRadius,
CGGradientDrawingOptions options
);
CGContextリファレンス またはCGGradientリファレンスの下のリンクを参照してください
簡単な方法
1:Create a image in photoshop with the alpha values which you have specified.
2:Place the new imageview over your image .
3:If you want to move the transparent box ,change the image frame.
4:If you want to expand you just change the size of upper image.