ios – 可以将视图放在彼此之上

ios – 可以将视图放在彼此之上,第1张

概述我正在构建一个Watch应用程序,其中我要覆盖WKInterface Image与一组WKInterfaceLabel对象.在StoryBoard编辑器中似乎无法做到这一点. 有没有人能够为Watch App设计出相互之间的观点? PS.我知道WKInterfaceGroup setBackgroundImage方法.因为我想在WKInterfaceImage里面做一些动画,所以setBackgr 我正在构建一个Watch应用程序,其中我要覆盖WKInterface Image与一组WKInterfaceLabel对象.在StoryBoard编辑器中似乎无法做到这一点.

有没有人能够为Watch App设计出相互之间的观点?

PS.我知道WKInterfaceGroup setBackgroundImage方法.因为我想在WKInterfaceImage里面做一些动画,所以setBackgroundImage不会让我失望

解决方法 您无法将WKInterfaceObjects布置在彼此之上.您可以将标签渲染在图像上,然后进行设置.您将必须为动画的每个帧渲染标签.我在手表应用程序中执行此按钮.我生成一个我的UI的大块图像,然后生成动画的每一帧,并覆盖每个帧的按钮文本.然后剪切每个框架的图像,以便我可以在每个按钮上的动画.当您使用该应用程序时,它看起来像是在按钮下的动画,实际上它是4种不同的动画在4个不同的按钮.

编辑

我想我会添加一些更多的细节.这是我的应用程序的屏幕截图.我猜你想做类似的事情.

首先在我的故事板上,您需要确保您的组的间距为零.

要创建这个UI,我使用这个助手类.我已经编辑了这个课程,只关注所需的部分.

typedef struct{    CGRect top;    CGRect left;    CGRect right;    CGRect bottom;} buttonRects;@interface GPWatchMAPImage ()@property (reaDWrite,nonatomic) UIImage* topImage;@property (reaDWrite,nonatomic) UIImage* bottomImage;@property (reaDWrite,nonatomic) UIImage* leftimage;@property (reaDWrite,nonatomic) UIImage* rightimage;@property (reaDWrite,nonatomic) CGRect topRect;@property (reaDWrite,nonatomic) CGRect bottomrect;@property (reaDWrite,nonatomic) CGRect leftRect;@property (reaDWrite,nonatomic) CGRect rightRect;@property (reaDWrite,nonatomic) UIImage* fullimageWithoutArrows;@property BOol addedArrows;@end@implementation GPWatchMAPImage-(instancetype)init{    self = [super init];    if (self)    {    }    return self;}-(UIImage*)leftimage{    if (_leftimage == nil)    {        [self breakUpForbuttons];    }    return _leftimage;}-(UIImage*)rightimage{    if (_rightimage == nil)    {        [self breakUpForbuttons];    }    return _rightimage;}-(UIImage*)topImage{    if (_topImage == nil)    {        [self breakUpForbuttons];    }    return _topImage;}-(UIImage*)bottomImage{    if (_bottomImage == nil)    {        [self breakUpForbuttons];    }    return _bottomImage;}-(UIImage*)fullimageWithoutArrows{    [self fullimage]; //make sure we have the full image    if (_fullimageWithoutArrows != nil)    {        return _fullimageWithoutArrows;    }    return _fullimage;}-(UIImage*)fullimage{    if (_fullimage == nil)    {        //This is the rect to create the image in        CGRect rect = CGRectMake(0,self.watchSize.wIDth,self.watchSize.height);        //This is how I generate map images.  You will need to do something else        self.generatedMAPInfo = [[GPCustomMapMaker instance] makeCustomMapFromConfig:self.mapConfig];        _fullimage = self.generatedMAPInfo.mAPImage;    }    if (self.showMapArrows && !self.addedArrows)    {        //Add the arrows        [self addbuttonArrowsToFullimage];    }    return _fullimage;}-(voID)addbuttonArrowsToFullimage{    self.addedArrows = YES;    buttonRects rects = [self buttonRects];    UIImage* img = self.fullimage;    self.fullimageWithoutArrows = img;  //save for animations    UIGraphicsBeginImageContext(img.size);    UIcolor* color = [UIcolor colorWithRed:.4 green:.4 blue:.4 Alpha:.6];    //CGSize arrowSize = CGSizeMake(24,4);    CGSize arrowSize = CGSizeMake(48,8);    CGfloat edgeOffset = 26;    CGContextRef ctx = UIGraphicsGetCurrentContext();    CGContextSetlinewidth(ctx,6);    CGContextSetlineJoin(ctx,kCGlineJoinRound);    CGContextSetlineCap(ctx,kCGlineCapRound);    CGContextSetstrokecolorWithcolor(ctx,color.CGcolor);    [img drawAtPoint:CGPointMake(0,0)];    //left arrow    CGPoint leftCenter = CGPointMake(rects.left.origin.x + edgeOffset,rects.left.origin.y + rects.left.size.height/2);    CGContextBeginPath(ctx);    CGContextMovetoPoint(ctx,leftCenter.x + arrowSize.height,leftCenter.y - arrowSize.wIDth/2);    CGContextAddlinetoPoint(ctx,leftCenter.x,leftCenter.y);    CGContextAddlinetoPoint(ctx,leftCenter.y + arrowSize.wIDth/2);    CGContextstrokePath(ctx);    CGPoint rightCenter = CGPointMake(rects.right.origin.x + rects.right.size.wIDth - edgeOffset,rects.right.origin.y + rects.right.size.height/2);    CGContextBeginPath(ctx);    CGContextMovetoPoint(ctx,rightCenter.x,rightCenter.y - arrowSize.wIDth/2);    CGContextAddlinetoPoint(ctx,rightCenter.x + arrowSize.height,rightCenter.y);    CGContextAddlinetoPoint(ctx,rightCenter.y + arrowSize.wIDth/2);    CGContextstrokePath(ctx);    CGPoint topCenter = CGPointMake(rects.top.origin.x + rects.top.size.wIDth/2,rects.top.origin.y + edgeOffset);    CGContextBeginPath(ctx);    CGContextMovetoPoint(ctx,topCenter.x - arrowSize.wIDth/2,topCenter.y + arrowSize.height);    CGContextAddlinetoPoint(ctx,topCenter.x,topCenter.y);    CGContextAddlinetoPoint(ctx,topCenter.x + arrowSize.wIDth/2,topCenter.y + arrowSize.height);    CGContextstrokePath(ctx);    CGPoint bottomCenter = CGPointMake(rects.bottom.origin.x + rects.bottom.size.wIDth/2,rects.bottom.origin.y + rects.bottom.size.height - edgeOffset);    CGContextBeginPath(ctx);    CGContextMovetoPoint(ctx,bottomCenter.x - arrowSize.wIDth/2,bottomCenter.y);    CGContextAddlinetoPoint(ctx,bottomCenter.x,bottomCenter.y + arrowSize.height);    CGContextAddlinetoPoint(ctx,bottomCenter.x + arrowSize.wIDth/2,bottomCenter.y);    CGContextstrokePath(ctx);    UIImage* imgWithbuttons = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();    _fullimage = imgWithbuttons;}-(UIImage*)subImageInRect:(CGRect)rect fromImage:(UIImage*)image{    UIGraphicsBeginImageContext(rect.size);    [image drawInRect:CGRectMake(-rect.origin.x,-rect.origin.y,image.size.wIDth,image.size.height)];    UIImage* newImage = UIGraphicsGetimageFromCurrentimageContext();    UIGraphicsEndImageContext();    return newImage;}-(buttonRects)buttonRects{    UIImage* img = self.fullimage;    CGSize size = self.watchSize;    CGfloat topHeight = size.height * .25;    CGfloat bottomHeight = size.height * .25;    CGfloat mIDdleHeight = size.height * .5;    CGRect topRect = CGRectMake(0,size.wIDth,topHeight);    CGRect leftRect = CGRectMake(0,topHeight,img.size.wIDth/2.0,mIDdleHeight);    CGRect rightRect = CGRectMake(img.size.wIDth/2.0,mIDdleHeight);    CGRect bottomrect = CGRectMake(0,topHeight + mIDdleHeight,bottomHeight);    buttonRects rects;    rects.top = topRect;    rects.bottom = bottomrect;    rects.left = leftRect;    rects.right = rightRect;    return rects;}-(voID)breakUpForbuttons{    UIImage* img = self.fullimage;    buttonRects rects = [self buttonRects];    _topImage = [self subImageInRect:rects.top fromImage:img];    _leftimage = [self subImageInRect:rects.left fromImage:img];    _rightimage = [self subImageInRect:rects.right fromImage:img];    _bottomImage = [self subImageInRect:rects.bottom fromImage:img];}@end

接下来在我的WKInterfaceController类中,我做这个动画.

typedef NS_ENUM(NSInteger,GPWatchImageAnimation){    GPWatchImageAnimationNone,GPWatchImageAnimationSlIDeleftToRight,GPWatchImageAnimationSlIDeRighToleft,GPWatchImageAnimationSlIDetopToBottom,GPWatchImageAnimationSlIDeBottomTotop,GPWatchImageAnimationZoomIn,GPWatchImageAnimationZoomOut};#define kNumImagesInMapAnimations 6#define kMapAnimatinonDuration 0.4...-(voID)updateMAPImageWithAnimation:(GPWatchImageAnimation)animation{    GPWatchMAPImage* prevIoUsImage = self.currentMAPImage;    //This gets the size of the full image that you want        CGSize size = [self mapSize];    self.currentMAPImage = [[GPWatchMAPImage alloc] init];    self.currentMAPImage.watchSize = size;        //Check if we have a prevIoUs image to animate from        if (prevIoUsImage != nil && animation != GPWatchImageAnimationNone)        {            NSDictionary* animatedImage = [self animateFromImage:prevIoUsImage toImage:self.currentMAPImage withAnimation:animation];            [self.maptopImage setimage:[animatedImage objectForKey:@"top"]];            [self.mapLeftimage setimage:[animatedImage objectForKey:@"left"]];            [self.mapRightimage setimage:[animatedImage objectForKey:@"right"]];            [self.mapBottomImage setimage:[animatedImage objectForKey:@"bottom"]];            [self.maptopImage startAnimatingWithImagesInRange:NSMakeRange(0,kNumImagesInMapAnimations) duration:kMapAnimatinonDuration repeatCount:1];            [self.mapLeftimage startAnimatingWithImagesInRange:NSMakeRange(0,kNumImagesInMapAnimations) duration:kMapAnimatinonDuration repeatCount:1];            [self.mapRightimage startAnimatingWithImagesInRange:NSMakeRange(0,kNumImagesInMapAnimations) duration:kMapAnimatinonDuration repeatCount:1];            [self.mapBottomImage startAnimatingWithImagesInRange:NSMakeRange(0,kNumImagesInMapAnimations) duration:kMapAnimatinonDuration repeatCount:1];        }        else        {            [self.maptopImage setimage:self.currentMAPImage.topImage];            [self.mapLeftimage setimage:self.currentMAPImage.leftimage];            [self.mapRightimage setimage:self.currentMAPImage.rightimage];            [self.mapBottomImage setimage:self.currentMAPImage.bottomImage];        }}-(NSDictionary*)animateFromImage:(GPWatchMAPImage*)fromImage toImage:(GPWatchMAPImage*)toImage withAnimation:(GPWatchImageAnimation)animation{    NSMutableArray* topAnimatedImages = [[NSMutableArray alloc] initWithCapacity:kNumImagesInMapAnimations];    NSMutableArray* bottomAnimatedImages = [[NSMutableArray alloc] initWithCapacity:kNumImagesInMapAnimations];    NSMutableArray* leftAnimatedImages = [[NSMutableArray alloc] initWithCapacity:kNumImagesInMapAnimations];    NSMutableArray* rightAnimatedImages = [[NSMutableArray alloc] initWithCapacity:kNumImagesInMapAnimations];    CGSize size = fromImage.fullimage.size;    for (int step = 0; step < kNumImagesInMapAnimations; step++)    {        UIGraphicsBeginImageContext(size);        //render this step        if (animation == GPWatchImageAnimationSlIDeleftToRight)        {            CGfloat stepSize = (size.wIDth/2)/kNumImagesInMapAnimations;            CGPoint fromPoint = CGPointMake(-(step+1)*stepSize,0);            CGPoint topoint = CGPointMake(size.wIDth/2 - (step+1)*stepSize,0);            [fromImage.fullimageWithoutArrows drawAtPoint:fromPoint];            [toImage.fullimageWithoutArrows drawAtPoint:topoint];        }        else if (animation == GPWatchImageAnimationSlIDeRighToleft)        {            CGfloat stepSize = (size.wIDth/2)/kNumImagesInMapAnimations;            CGPoint fromPoint = CGPointMake((step+1)*stepSize,0);            CGPoint topoint = CGPointMake(-size.wIDth/2 + (step+1)*stepSize,0);            [fromImage.fullimageWithoutArrows drawAtPoint:fromPoint];            [toImage.fullimageWithoutArrows drawAtPoint:topoint];        }        else if (animation == GPWatchImageAnimationSlIDetopToBottom)        {            CGfloat stepSize = (size.height/2)/kNumImagesInMapAnimations;            CGPoint fromPoint = CGPointMake(0,(step+1)*stepSize);            CGPoint topoint = CGPointMake(0,-size.height/2 + (step+1)*stepSize);            [fromImage.fullimageWithoutArrows drawAtPoint:fromPoint];            [toImage.fullimageWithoutArrows drawAtPoint:topoint];        }        else if (animation == GPWatchImageAnimationSlIDeBottomTotop)        {            CGfloat stepSize = (size.height/2)/kNumImagesInMapAnimations;            CGPoint fromPoint = CGPointMake(0,-(step+1)*stepSize);            CGPoint topoint = CGPointMake(0,size.height/2 - (step+1)*stepSize);            [fromImage.fullimageWithoutArrows drawAtPoint:fromPoint];            [toImage.fullimageWithoutArrows drawAtPoint:topoint];        }        else if (animation == GPWatchImageAnimationZoomOut)        {            CGfloat yStepSize = (size.height/2)/kNumImagesInMapAnimations;            CGfloat xStepSize = (size.wIDth/2)/kNumImagesInMapAnimations;            //CGRect fromrect = CGRectMake((step + 1)*xStepSize,(step + 1)*yStepSize,size.wIDth - 2*(step + 1)*xStepSize,size.height - 2*(step + 1)*yStepSize);            CGRect toRect = CGRectMake(-size.wIDth/2 + (step+1)*xStepSize,-size.height/2 + (step+1)*yStepSize,size.wIDth + 2*(kNumImagesInMapAnimations - step - 1)*xStepSize,size.height + 2*(kNumImagesInMapAnimations - step - 1)*yStepSize);            [toImage.fullimageWithoutArrows drawInRect:toRect];            //double Alpha = (double)(kNumImagesInMapAnimations - step - 1)/(double)kNumImagesInMapAnimations;            //CGContextSetAlpha(UIGraphicsGetCurrentContext(),Alpha);            //[fromImage.fullimageWithoutArrows drawInRect:fromrect];            //CGContextSetAlpha(UIGraphicsGetCurrentContext(),1.0);        }        else if (animation == GPWatchImageAnimationZoomIn)        {            if (step == kNumImagesInMapAnimations -1)            {                [toImage.fullimageWithoutArrows drawAtPoint:CGPointMake(0,0)];            }            else            {                CGfloat yStepSize = (size.height/2)/kNumImagesInMapAnimations;                CGfloat xStepSize = (size.wIDth/2)/kNumImagesInMapAnimations;                CGRect fromrect = CGRectMake(-(step + 1)*xStepSize,-(step + 1)*yStepSize,size.wIDth + 2*(step + 1)*xStepSize,size.height + 2*(step + 1)*yStepSize);                //CGRect toRect = CGRectMake(-size.wIDth/2 + (step+1)*xStepSize,size.height + 2*(kNumImagesInMapAnimations - step - 1)*yStepSize);                [fromImage.fullimageWithoutArrows drawInRect:fromrect];                //[toImage.fullimageWithoutArrows drawInRect:fromrect];            }        }        else        {            [toImage.fullimageWithoutArrows drawAtPoint:CGPointMake(0,0)];        }        UIImage* stepimg = UIGraphicsGetimageFromCurrentimageContext();        UIGraphicsEndImageContext();        //Get each of the pIEces of the image        GPWatchMAPImage* mAPImage = [[GPWatchMAPImage alloc] init];        mAPImage.showMapArrows = self.showMapArrows;        mAPImage.fullimage = stepimg;        mAPImage.watchSize = [self mapSize];        [topAnimatedImages addobject:mAPImage.topImage];        [bottomAnimatedImages addobject:mAPImage.bottomImage];        [leftAnimatedImages addobject:mAPImage.leftimage];        [rightAnimatedImages addobject:mAPImage.rightimage];    }    UIImage* topAnimatedImage = [UIImage animatedImageWithImages:topAnimatedImages duration:kMapAnimatinonDuration];    UIImage* bottomAnimatedImage = [UIImage animatedImageWithImages:bottomAnimatedImages duration:kMapAnimatinonDuration];    UIImage* leftAnimatedImage = [UIImage animatedImageWithImages:leftAnimatedImages duration:kMapAnimatinonDuration];    UIImage* rightAnimatedImage = [UIImage animatedImageWithImages:rightAnimatedImages duration:kMapAnimatinonDuration];    return @{@"top": topAnimatedImage,@"bottom": bottomAnimatedImage,@"left": leftAnimatedImage,@"right": rightAnimatedImage};}
总结

以上是内存溢出为你收集整理的ios – 可以将视图放在彼此之上全部内容,希望文章能够帮你解决ios – 可以将视图放在彼此之上所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存