Can't use rotation effect in clip shape in swiftUI - ios

This i my code:
Image()
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.clipShape(Rectangle().cornerRadius(8).rotationEffect(.degrees(45)))
I receive this error:
Instance method 'clipShape(_:style:)' requires that 'some View' conform to 'Shape'
Or you know another way to crop my image to diamond?

Like this?
You can encapsulate the image in a ZStack and rotate them opposite to each other. Then set the frame of the ZStack to match the image. Feel free to ask if anything is unclear!
ZStack {
Image("IMG_1544")
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.rotationEffect(.degrees(-45))
}
.frame(width: cos(.pi/4) * 200, height: sin(.pi/4) * 200)
.cornerRadius(8)
.rotationEffect(.degrees(45))

Try to use mask instead, maybe it will be enough for your needs, and it accepts views, like
Image()
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.mask(Rectangle().cornerRadius(8).rotationEffect(.degrees(45))) // << here !!

Related

How to indent first line of Label in SwiftUI?

I simply need to achieve something like this:
using:
ZStack {
Image.book
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 12, height: 10)
Text(verbatim: entry.verse)
.font(.openSansRegular(withSize: 11))
}
But have no idea how to achieve it.
Similar question but for UILabel was here

How to properly fit a filled Image in its bounds on SwiftUI?

I have to fix a problem with Images on SwiftUI. I currently have to fill the images I receive in a certain vertical container, which are the cells of a LazyVGrid, but the problem is that the tappable area expands over the designated one.
I used the Accessibility Inspector, and I found out that the area in excess is due the Images. (screenshot attached)
The code I'm using is something similar to the following:
Image(uiImage: image.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(
maxWidth: width,
maxHeight: height
)
.clipped()
I thought that by having the frames and the clipped method I could get rid of this problem, but that's not the case unless I'm using some squared images. Any idea of what could work to fix this problem? Thanks!
Just add .contentShape(Rectangle()) at the bottom:
Image(uiImage: image.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(
maxWidth: width,
maxHeight: height
)
.clipped()
.contentShape(Rectangle()) // <- Here!
Wrap content in Geometry Reader and you need to know the aspect ratio like I set 720/460 below
GeometryReader { gr in
Image(uiImage: image.image)
.resizable()
.aspectRatio(720/460, contentMode: .fit)
}
.clipped()
.aspectRatio(720/460, contentMode: .fit)

SwiftUI - How to apply aspectFit to image so it does not go outside of designated area

I am trying to fit a square shaped image in rectangle shaped ImageView. With storyboard, I can easily do it with content mode .aspectFit but I am unable to figure out how to do it with SwiftUI.
I am currently doing something like this:
Image("BG")
.resizable()
.aspectRatio(contentMode: .fill)
.scaledToFill()
.clipped()
.frame(height: 200)
which gives below output, notice the blue border is my actual ImageView:
However, I am expecting something like this which I did in storyboard:
You need to clip it after set size, like
Image("BG")
.resizable()
.aspectRatio(contentMode: .fill)
.scaledToFill()
.frame(height: 200)
.clipped() // << here !!

XCode (Bug?) (SwiftUI): Rather than Scale and Change Opacity where they are, my Views are coming in from the edge of the screen

I am trying to have a circle on my screen with two circles behind it in a ZStack that ease in and out with the ScaleEffect(), and change opacity. I made this in a separate SwiftUI file, where there seemed to be no issues, but once I put it in my ContentView() it this weird bug seemed to occur.
Please ignore the circles in the background, that's just my background view. But see those two circles of slightly different shades of blue? They keep entering and exiting the screen, coming behind the dark blue "plus" icon and then leaving again. Meanwhile, I'd like them to simply be behind the "plus" circle.
enter image description here
enter image description here
enter image description here
Is this because of some kind of XCode bug? Or did I write something wrong in my code?
I would really appreciate it if somebody could clarify :)
Here is my code. I made an #State private var buttonIsAnimating and defaulted it to false, and said that once the button appears, the circles should start animating. Is something wrong with the code?
ZStack {
Group {
Circle()
.fill(Color("Background2").opacity(self.buttonIsAnimating ? 0.6 : 0))
.frame(width: 75, height: 75, alignment: .center)
.scaleEffect(self.buttonIsAnimating ? 1 : 0)
Circle()
.fill(Color("Background3").opacity(self.buttonIsAnimating ? 0.7 : 0))
.frame(width: 89, height: 89, alignment: .center)
.scaleEffect(self.buttonIsAnimating ? 1 : 0)
}
.animation(Animation.easeInOut(duration: 2).repeatForever(autoreverses: true))
Button(action: {
self.showingAddANewToDoView.toggle()
}) {
Image(systemName: "plus.circle.fill")
.resizable()
.scaledToFit()
.background(Circle().fill(Color("Background")))
.foregroundColor(Color("Background4"))
.frame(width: 60, height: 60)
.padding(5)
}//: Button
.onAppear {
self.buttonIsAnimating.toggle()
}
}
Try with link animation to state, like
Group {
Circle()
.fill(Color("Background2").opacity(self.buttonIsAnimating ? 0.6 : 0))
.frame(width: 75, height: 75, alignment: .center)
.scaleEffect(self.buttonIsAnimating ? 1 : 0)
Circle()
.fill(Color("Background3").opacity(self.buttonIsAnimating ? 0.7 : 0))
.frame(width: 89, height: 89, alignment: .center)
.scaleEffect(self.buttonIsAnimating ? 1 : 0)
}
.animation(Animation.easeInOut(duration: 2).repeatForever(autoreverses: true),
value: self.buttonIsAnimating)

SwiftUI Circle() decreases performance in List

I've a list of video, the image of these video are loaded in async way. In the mosaic I can see the activity indicator if is still loading, after the image was loaded, I can see the image preview. I've added an icon with play button.
When I scroll the list, it is not fluid. I've tried to change all async process, but without success. After some time I've tried to delete the Circle() function and was exactly it to decreases the performance.
Even if I solved the issue by changing the icon, I'd like to know if also you have this problem with circle function. It sounds like SKShapeNode in sprite kit.
Bad performance:
ZStack {
Circle()
.fill(Color.white)
.frame(width: 55, height: 55)
.opacity(0.7)
Image(systemName: "play.fill")
.font(Font.system(size: 33))
.opacity(0.7)
.foregroundColor(.black)})
}
Good performance:
Image(systemName: "play.circle.fill")
.resizable()
.frame(width: 50, height: 50)
.opacity(0.7)
.foregroundColor(Color.white)

Resources