【问题标题】:How to apply corner radius to a UIView without issues?如何在没有问题的情况下将角半径应用于 UIView?
【发布时间】:2013-05-12 03:46:36
【问题描述】:

目前我正在将角半径和阴影应用于滚动视图中的多个 UIView。我注意到添加角半径和阴影会使滚动视图在滚动时变得疯狂。如何应用这些影响而不影响我的表现?

【问题讨论】:

    标签: iphone uiscrollview shadow cornerradius


    【解决方案1】:

    尝试同时设置图层的shadowPath:

    view.layer.cornerRadius=6.0f;
    view.layer.borderWidth=2.0f;
    view.layer.borderColor=[UIColor grayColor].CGColor;
    view.layer.shadowColor = [UIColor blackColor].CGColor;
    view.layer.shadowOpacity = 0.3f;
    view.layer.shadowOffset = CGSizeMake(0, 0.0f);
    view.layer.masksToBounds = NO;
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
    view.layer.shadowPath = path.CGPath;
    

    【讨论】:

      【解决方案2】:
      +(UIImage *)makeRoundCornerImage : (UIImage*) img : (int) cornerWidth : (int) cornerHeight
      {
          UIImage * newImage = nil;
      
          if( nil != img)
          {
              NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
              int w = img.size.width;
              int h = img.size.height;
      
              CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
              CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
      
              CGContextBeginPath(context);
              CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
              addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
              CGContextClosePath(context);
              CGContextClip(context);
      
              CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
      
              CGImageRef imageMasked = CGBitmapContextCreateImage(context);
              CGContextRelease(context);
              CGColorSpaceRelease(colorSpace);
              [img release];
      
      
              newImage = [[UIImage imageWithCGImage:imageMasked] retain];
              CGImageRelease(imageMasked);
      
              [pool release];
          }
      
          return newImage;
      }
      

      // 借助这个方法我们可以创建任意PNG文件的圆角

       UIImage *imageFromFile = [UIImage imageNamed:@"FilterFields.png"];
              imageFromFile = [ImageManipulator
      makeRoundCornerImage:imageFromFile : 10 : 10];
      
      
              [SmallTable setImage:imageFromFile];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2012-10-21
        • 2012-04-21
        • 1970-01-01
        相关资源
        最近更新 更多